为什么我收到 java.lang.NullPointerException 而不是soap响应?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19953829/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why am I receiving java.lang.NullPointerException rather than soap response?
提问by
I am sending the following request to the server, when I copy the request and use the SOAPUI it shows the correct response,
我正在向服务器发送以下请求,当我复制请求并使用 SOAPUI 时,它显示了正确的响应,
but when I use the following code to generate and send it, returns
但是当我使用以下代码生成并发送它时,返回
java.lang.NullPointerException
on line 50, which is
在第 50 行,即
sm.writeTo(out);
code:
代码:
SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
SOAPConnection connection = soapConnectionFactory.createConnection();
SOAPFactory soapFactory = SOAPFactory.newInstance();
MessageFactory factory = MessageFactory.newInstance();
SOAPMessage message = factory.createMessage();
SOAPHeader header = message.getSOAPHeader();
SOAPPart soapPart = message.getSOAPPart();
SOAPEnvelope soapEnvelope = soapPart.getEnvelope();
SOAPBody body = soapEnvelope.getBody();
header.removeNamespaceDeclaration(header.getPrefix());
soapEnvelope.addNamespaceDeclaration("v9", "ADDRESS OF SERVICE");
Name bodyName;
bodyName = soapFactory.createName("User");
SOAPElement getList = body.addChildElement("User", "v9");
Name childName;
getList.addChildElement("name", "v9").addTextNode("Alex");
getList.addChildElement("surname", "v9").addTextNode("Nicroid");
message.writeTo(System.out);
URL endpoint = new URL("ENDPOINT ADDRESS OF SERVER");
SOAPMessage response = connection.call(message, endpoint);
connection.close();
SOAPMessage sm = response;
ByteArrayOutputStream out = new ByteArrayOutputStream();
sm.writeTo(out); //java.lang.NullPointerException
System.out.println(out.toString());
Maven
马文
<url>http://maven.apache.org</url>
<build>
<resources>
<resource>
<targetPath>META-INF</targetPath>
<directory>src</directory>
<includes>
<include>jax-ws-catalog.xml</include>
<include>wsdl/**</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.jvnet.jax-ws-commons</groupId>
<artifactId>jaxws-maven-plugin</artifactId>
<version>2.2.1</version>
<dependencies>
<dependency>
<groupId>javax.xml</groupId>
<artifactId>webservices-api</artifactId>
<version>1.4</version>
</dependency>
</dependencies>
<configuration>
<sourceDestDir>${project.build.directory}/generated-sources/jaxws-wsimport</sourceDestDir>
<xnocompile>true</xnocompile>
<verbose>true</verbose>
<extension>true</extension>
<catalog>${basedir}/src/jax-ws-catalog.xml</catalog>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jdom</groupId>
<artifactId>jdom</artifactId>
<version>1.1</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>com.sun.xml.ws</groupId>
<artifactId>webservices-rt</artifactId>
<version>1.4</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
回答by Chaffers
SOAPConnection.call() is abstract is it not?
SOAPConnection.call() 是抽象的不是吗?
You must subclass the class, provide concrete implementations of it's abstract methods and then instantiate it.
您必须对类进行子类化,提供其抽象方法的具体实现,然后实例化它。
Otherwise you will end up with NPEs as you cannot instantiate an abstract class.
否则,您最终会遇到 NPE,因为您无法实例化抽象类。
回答by DYezek
The class you are using, SOAPMessage, is abstract, as is the method writeTo(). This means there is no code other than the method definition (similar to a function prototype in C). The insides (inside the curly braces of the method) are empty. To fix your issue, you need to extend the SOAPMessage class in a new class and provide implementation for the methods therein.
您正在使用的类 SOAPMessage 是抽象的,方法 writeTo() 也是如此。这意味着除了方法定义之外没有其他代码(类似于 C 中的函数原型)。内部(方法的花括号内)是空的。要解决您的问题,您需要在新类中扩展 SOAPMessage 类并提供其中方法的实现。
public class myMessage extends SOAPMessage {
public void writeTo(OutputStream out) throws SOAPException, IOException {
// your code goes here
}
// All other methods in the superclass implemented here.
}
Basics of abstraction here: http://docs.oracle.com/javase/tutorial/java/IandI/abstract.html
这里的抽象基础:http: //docs.oracle.com/javase/tutorial/java/IandI/abstract.html
回答by rajivas
check whether sm is null by placing system.out.println(" soap message:" +sm)
通过放置 system.out.println(" soap message:" +sm) 检查 sm 是否为空
if response is null check what this message.writeTo(System.out); statement will print. It will also give hint about your soap request is well formed or not
如果响应为空,请检查此 message.writeTo(System.out); 语句将打印。它还会提示您的肥皂请求是否格式正确
capture the low level http request you are sending to the endpoint using eclipse TCP/IP monitor or through other means (enable trace logging). most probably your SOAP request might not be well formed so, server failing to send response back.
使用 eclipse TCP/IP 监视器或通过其他方式(启用跟踪日志记录)捕获您发送到端点的低级 http 请求。很可能您的 SOAP 请求可能格式不正确,因此服务器无法发回响应。
回答by Dario
If you are having NullPointerException at line 50 it's because stis null. That is connection.call(message, endpoint);is returning null.
如果您在第 50 行遇到 NullPointerException,那是因为st为空。那就是connection.call(message, endpoint); 正在返回空值。
Assuming you are using the default implementation of SOAPConnection (com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnectionFactory - See http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/javax/xml/soap/SOAPConnectionFactory.java- Lines 47-48) There are just a few reasons method call(...)can return null:
假设您使用 SOAPConnection 的默认实现(com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnectionFactory - 请参阅http://grepcode.com/file/repository.grepcode.com/java/root/jdk /openjdk/6-b14/javax/xml/soap/SOAPConnectionFactory.java- 第 47-48 行)方法call(...)可以返回 null 的原因有几个:
- You are receiving a response code that is neither HttpURLConnection.HTTP_INTERNAL_ERROR (500) nor HttpURLConnection.HTTP_OK (200)
- The response code is correct but no reply message is returned
- 您收到的响应代码既不是 HttpURLConnection.HTTP_INTERNAL_ERROR (500) 也不是 HttpURLConnection.HTTP_OK (200)
- 响应码正确但未返回回复消息
I can't go any further with the analysis because I'm not able to reproduce the issue. I suggest you download source code of involved java classes, link it to your debugger and execute it in debug mode to see what is actually happening in your usecase.
我无法进一步分析,因为我无法重现该问题。我建议您下载相关 java 类的源代码,将其链接到您的调试器并在调试模式下执行它以查看您的用例中实际发生的情况。