WebService调用时空指针异常解决方案

由于对WebService了解不深,在工作中首次碰到,调用时出现了如下异常,异常提示信息如下:
Exception in thread "main" AxisFault
 faultCode: {http://schemas.xmlsoap.org/soap/envelope/}Server.userException
 faultSubcode: 
 faultString: java.lang.NullPointerException
 faultActor: 
 faultNode: 
 faultDetail: 
	{}faultData:null
	{http://xml.apache.org/axis/}hostname:test

java.lang.NullPointerException
	at org.apache.axis.message.SOAPFaultBuilder.createFault(SOAPFaultBuilder.java:222)
	at org.apache.axis.message.SOAPFaultBuilder.endElement(SOAPFaultBuilder.java:129)
	at org.apache.axis.encoding.DeserializationContext.endElement(DeserializationContext.java:1087)
	at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.endElement(AbstractSAXParser.java:601)
	at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanEndElement(XMLDocumentFragmentScannerImpl.java:1782)
	at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2938)
	at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:648)
	at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:140)
	at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:511)
	at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:808)
	at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737)
	at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:119)
	at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1205)
	at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:522)
	at javax.xml.parsers.SAXParser.parse(SAXParser.java:395)
	at org.apache.axis.encoding.DeserializationContext.parse(DeserializationContext.java:227)
	at org.apache.axis.SOAPPart.getAsSOAPEnvelope(SOAPPart.java:696)
	at org.apache.axis.Message.getSOAPEnvelope(Message.java:435)
	at org.apache.axis.handlers.soap.MustUnderstandChecker.invoke(MustUnderstandChecker.java:62)
	at org.apache.axis.client.AxisClient.invoke(AxisClient.java:206)
	at org.apache.axis.client.Call.invokeEngine(Call.java:2784)
	at org.apache.axis.client.Call.invoke(Call.java:2767)
	at org.apache.axis.client.Call.invoke(Call.java:2443)
	at org.apache.axis.client.Call.invoke(Call.java:2366)
	at org.apache.axis.client.Call.invoke(Call.java:1812)
	at com.jira.webservice.client.JirasoapserviceV2SoapBindingStub.getIssue(JirasoapserviceV2SoapBindingStub.java:3727)
	at com.test.Test.main(Test.java:17)

   这异常困扰了我整整两天,在网上百度和谷歌的前几页相关的资料都翻遍了也没找到有效的解决方案,更多的是有人提问没人回答。问题到底出在哪,有人说版本的问题,好,我换jdk,我换Tomcat,换axis的jar包,可异常还是稳如泰山,可恶可恨,无助无奈。我下axis源代码,步步调试,发现问题是出在调用invoke方法上,可看来看去参数也不会为空;从异常来看userException,我有填用户名和密码啊,空指针到底什么为空?没办法,要有耐心继续调试,想到一种方法就用一种,偶然间我调用其它方法发现可以正常运行,这下我看到了希望,我将原来的方法换个参数试试也能正常运行,这下我看到了光明。离晴天仅一步之遥,却无法前进一步,问题实在太怪了,没办法只好求助经理,经理来了,调了几下,也觉得很怪,但经理毕竟有经验啊,我只停留在客户端代码的调试,根本就没想到服务端代码的调试,经理将日志改为debug模式,重新运行查看日志,结果很快就看到了有一处报userException,具体的哪一行代码出错很显眼显示在上面。找到相应的代码,发现原来是那个参数所对应的代码调用了ActionContext来获得user,而我用WebService来调用是没有初始化ActionContext,所以取到的user也是空的,所以报了空指针,修改后就没有问题了。

你可能感兴趣的:(java,webservice)