2013-07-24 71 views
0

這是一個示例類我不知道爲什麼本網站服務無法正常工作

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) 
    . 
    . 
    . 

請我需要了解此問題以及如何解決它。

+1

'newess'爲'null'。用newess = new NewClass()初始化它'' – jlordo

回答

-1

試試這樣說:

 this.newess = new NewClass(); 

這不是線程安全的。成員變量是不必要的。

不好的代碼,但我相信這只是一個壞例子。

+0

'NewClass'只有默認的構造函數。 – jlordo

+0

只需注意,謝謝jlordo。 – duffymo

+0

當然,我不是downvoter,但。 – jlordo

0

初始化您的NewClass newess。您嘗試訪問newess.hello1()而不初始化您的newess變量

相關問題