【发布时间】:2023-03-04 20:56:01
【问题描述】:
我在 ubuntu 下的 jboss 上部署了简单的“HelloWorld”网络服务。 我创建了简单的客户端,但我无法让它工作。每次运行客户端时都会收到 NullPointerException。
请注意,我在 Ubuntu 下的 Oracle Java 7 上运行。
代码如下: HelloWorldClient.java
import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
public class HelloWorldClient {
public static void main(String[] args){
URL url;
try {
url = new URL("http://localhost:8080/WebServiceProject/helloWorld?wsdl");
QName qname = new QName("http:///", "HelloWorldImplService");
Service service = Service.create(url, qname);
HelloWorld hello = service.getPort(HelloWorld.class);
System.out.println(hello.sayHello("mkyong"));
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
HelloWorld.java
import javax.jws.WebMethod;
import javax.jws.WebService;
@WebService
public interface HelloWorld {
@WebMethod
public String sayHello(String name);
}
堆栈跟踪:
Exception in thread "main" java.lang.NullPointerException
at com.sun.xml.internal.ws.model.RuntimeModeler.getPortTypeName(RuntimeModeler.java:1407)
at com.sun.xml.internal.ws.client.WSServiceDelegate.getPort(WSServiceDelegate.java:334)
at com.sun.xml.internal.ws.client.WSServiceDelegate.getPort(WSServiceDelegate.java:354)
at javax.xml.ws.Service.getPort(Service.java:188)
at HelloWorldClient.main(HelloWorldClient.java:18)
在这一行抛出异常:
HelloWorld hello = service.getPort(HelloWorld.class);
【问题讨论】:
-
只是一个简短的评论,可能与您的问题完全无关,但您的
QName有一个额外的/。是不是笔误? -
@SamRad 可能是相关的。将解释为什么
service可能为空 -
没有多余的“/”。你写像“something”这样的网址。在这种情况下,没有“东西”。服务也不为空。查看堆栈跟踪
-
您是从 ide 运行客户端吗?例如日食?如果是这样,请确保 jboss 使用的 jdk 与 eclipse 使用的 jdk 相同。也许一个正在运行 oracle jdk 7(正如你提到的)但 eclipse 正在运行 openjdk。
-
你使用哪个 JDK?我在 getPort 也遇到了空指针异常,我通过将我的 JDK 从 1.6 版本升级到 1.7 来解决它。希望它会有所帮助。
标签: web-services nullpointerexception jax-ws port