首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2EE开发 >

为什么客户端调用抛出java.lang.NullPointerException错误

2012-08-11 
为什么客户端调用抛出java.lang.NullPointerException异常?我用axis即时发布了一个简单的webservice,代码

为什么客户端调用抛出java.lang.NullPointerException异常?
我用axis即时发布了一个简单的webservice,代码是:
public   class   CalcService   {
public   int   add(int   p1,   int   p2)   {
return   p1   +   p2;
}
public   int   subtract(int   p1,   int   p2)   {
return   p1   -   p2;
}
}
输入http://localhost:8080/axis/CalcService.jws?WSDL后能正常看见内容:
<?xml   version= "1.0 "   encoding= "UTF-8 "   ?>  
-   <wsdl:definitions   targetNamespace= "http://localhost:8080/axis/CalcService.jws "   xmlns= "http://schemas.xmlsoap.org/wsdl/ "   xmlns:apachesoap= "http://xml.apache.org/xml-soap "   xmlns:impl= "http://localhost:8080/axis/CalcService.jws "   xmlns:intf= "http://localhost:8080/axis/CalcService.jws "   xmlns:soapenc= "http://schemas.xmlsoap.org/soap/encoding/ "   xmlns:wsdl= "http://schemas.xmlsoap.org/wsdl/ "   xmlns:wsdlsoap= "http://schemas.xmlsoap.org/wsdl/soap/ "   xmlns:xsd= "http://www.w3.org/2001/XMLSchema ">
-   <wsdl:message   name= "subtractResponse ">
    <wsdl:part   name= "subtractReturn "   type= "xsd:int "   />  
    </wsdl:message>
-   <wsdl:message   name= "subtractRequest ">
    <wsdl:part   name= "p1 "   type= "xsd:int "   />  
    <wsdl:part   name= "p2 "   type= "xsd:int "   />  
    </wsdl:message>
-   <wsdl:message   name= "addRequest ">
    <wsdl:part   name= "p1 "   type= "xsd:int "   />  
    <wsdl:part   name= "p2 "   type= "xsd:int "   />  
    </wsdl:message>
-   <wsdl:message   name= "addResponse ">
    <wsdl:part   name= "addReturn "   type= "xsd:int "   />  
    </wsdl:message>
-   <wsdl:portType   name= "CalcService ">
-   <wsdl:operation   name= "add "   parameterOrder= "p1   p2 ">
    <wsdl:input   message= "impl:addRequest "   name= "addRequest "   />  
    <wsdl:output   message= "impl:addResponse "   name= "addResponse "   />  
    </wsdl:operation>
-   <wsdl:operation   name= "subtract "   parameterOrder= "p1   p2 ">
    <wsdl:input   message= "impl:subtractRequest "   name= "subtractRequest "   />  
    <wsdl:output   message= "impl:subtractResponse "   name= "subtractResponse "   />  
    </wsdl:operation>
    </wsdl:portType>
-   <wsdl:binding   name= "CalcServiceSoapBinding "   type= "impl:CalcService ">
    <wsdlsoap:binding   style= "rpc "   transport= "http://schemas.xmlsoap.org/soap/http "   />  
-   <wsdl:operation   name= "add ">
    <wsdlsoap:operation   soapAction= " "   />  
-   <wsdl:input   name= "addRequest ">


    <wsdlsoap:body   encodingStyle= "http://schemas.xmlsoap.org/soap/encoding/ "   namespace= "http://localhost:8080/axis/CalcService.jws "   use= "encoded "   />  
    </wsdl:input>
-   <wsdl:output   name= "addResponse ">
    <wsdlsoap:body   encodingStyle= "http://schemas.xmlsoap.org/soap/encoding/ "   namespace= "http://localhost:8080/axis/CalcService.jws "   use= "encoded "   />  
    </wsdl:output>
    </wsdl:operation>
-   <wsdl:operation   name= "subtract ">
    <wsdlsoap:operation   soapAction= " "   />  
-   <wsdl:input   name= "subtractRequest ">
    <wsdlsoap:body   encodingStyle= "http://schemas.xmlsoap.org/soap/encoding/ "   namespace= "http://localhost:8080/axis/CalcService.jws "   use= "encoded "   />  
    </wsdl:input>
-   <wsdl:output   name= "subtractResponse ">
    <wsdlsoap:body   encodingStyle= "http://schemas.xmlsoap.org/soap/encoding/ "   namespace= "http://localhost:8080/axis/CalcService.jws "   use= "encoded "   />  
    </wsdl:output>
    </wsdl:operation>
    </wsdl:binding>
-   <wsdl:service   name= "CalcServiceService ">
-   <wsdl:port   binding= "impl:CalcServiceSoapBinding "   name= "CalcService ">
    <wsdlsoap:address   location= "http://localhost:8080/axis/CalcService.jws "   />  
    </wsdl:port>
    </wsdl:service>
    </wsdl:definitions>
但是我在客户端调用时,执行到invoke时就抛出NullPointerException异常,不能调用,我的客户端程序是:
import       java.util.Vector;
import       org.apache.axis.client.Call;  
import       org.apache.axis.client.Service;
import       org.apache.axis.encoding.XMLType;
import     org.apache.soap.rpc.Response;    
 
import     javax.xml.rpc.ParameterMode;  

public       class       TestClient       {
public       static       void       main(String       []       args)   {
try{
String   endpoint   =   "http://localhost:8080/axis/CalcService.jws ";
Service   service   =   new   Service();
Call   call   =   (Call)service.createCall();
call.setTargetEndpointAddress(   new   java.net.URL(endpoint)   );
int   i=1;
int   j=9;
call.setOperationName(       "add "       );
call.addParameter( "p1 ",XMLType.XSD_INT,ParameterMode.IN);
call.addParameter( "p2 ",XMLType.XSD_INT,ParameterMode.IN);
call.setReturnType(   XMLType.XSD_INT);
Object   object   =   (   call.invoke(   new   Object[]   {i,j}   ));
}   catch(Exception   e){
System.err.println(e.toString());
}
}
}      
小弟跪求高手指点。

------解决方案--------------------


空指针大多是你想传的参数没传到而引起的
包这个错的时候应该有提示你在哪一行上出了错
你好好的检查一下那几行

还有以后不要发这么长的代码,别人看了也会烦的
[解决办法]
我也想知道

热点排行