我不知道为什么这个Web服务不起作用

时间:2013-07-24 09:45:42

标签: java web-services

这是一个示例类

public class NewClass {
    public String hello1(String txt) {
        String a = "Hello " + txt + " !";
        return a;
    }
}

这是一个示例Web服务

@WebService(serviceName = "NewWebService2")
public class NewWebService2 {
    private NewClass newess;
    /**
     * This is a sample web service operation
     */
    @WebMethod(operationName = "hello")
    public String hello(@WebParam(name = "name") String txt) {
        return "Hello " + txt + " !";
    }

    @WebMethod(operationName = "hello11")
    public String hello11(@WebParam(name = "name") String txt) {
        String ess=newess.hello1(txt);
        return ess;
    }

}

第一种方法hello完全正常,但第二种方法hello11无效。我不明白这个问题。服务器显示以下消息:

SEVERE: java.lang.NullPointerException
at newpackage.NewWebService2.hello11(NewWebService2.java:31)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.glassfish.webservices.InstanceResolverImpl$1.invoke(InstanceResolverImpl.java:143)
at com.sun.xml.ws.server.InvokerTube$2.invoke(InvokerTube.java:149)
at com.sun.xml.ws.server.sei.SEIInvokerTube.processRequest(SEIInvokerTube.java:94)
at com.sun.xml.ws.api.pipe.Fiber.__doRun(Fiber.java:961)
    .
    .
    .

我需要了解这个问题以及如何解决它。

2 个答案:

答案 0 :(得分:0)

初始化 NewClass newess 。您正在尝试访问newess.hello1()而不初始化您的newess变量

答案 1 :(得分:-1)

试试这样:

      this.newess = new NewClass(); 

这不是线程安全的。成员变量是不必要的。

不是很好的代码,但我确定这只是一个糟糕的例子。

相关问题