5

I am sending the following request to the server, when I copy the request and use the SOAPUI it shows the correct response,

but when I use the following code to generate and send it, returns

java.lang.NullPointerException

on line 50, which is

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>
The Guy with The Hat
  • 10,836
  • 8
  • 57
  • 75
  • 1
    Could you please provide whole stacktrace? Because it's pretty similar to example of SAAJ from [Oracle's site](http://docs.oracle.com/javaee/1.4/tutorial/doc/SAAJ4.html), and it needs actual provider (`saaj-impl.jar` or alternative, such as [AXIS](http://axis.apache.org/axis/)) – n1ckolas Nov 13 '13 at 13:01
  • The method connection.call most probably returns null instead of the response. Please pusblish your stacktrace. – mwhs Nov 13 '13 at 13:06
  • @mwhs I used the same code for another webservice and worked. Not sure why it is showing this error. It just shows "java.lang.NullPointerException" –  Nov 14 '13 at 08:11
  • Try `sm.writeTo(System.out)`if that works. Maybe your OutputStream object `out` is null. – mwhs Nov 14 '13 at 08:15
  • @mwhs it returns same error –  Nov 14 '13 at 08:24
  • Then maybe it is the intended behaviour? What kind of operation is this? Is a response message defined in the interface? – mwhs Nov 14 '13 at 08:49
  • when I use the generated request in SOAPUI it correctly shows the response, and I am using the same code to generate a different request to different endpoint which works, but this one does not, it is weird. –  Nov 14 '13 at 08:52
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/41165/discussion-between-alex-and-mwhs) –  Nov 14 '13 at 08:58
  • 2
    On line 50 there are two objects that can be null: `sm` and `out`. Since `sm.writeTo(System.out);` gets the same error, `sm` is null. `sm` equals `response`, which equals `connection.call(message, endpoint);`. `.call()` returns "the SOAPMessage object that is the response to the message that was sent". Therefore, you are not receiving any response from your server. – The Guy with The Hat Nov 17 '13 at 22:45
  • @RyanCarlson I've recognized that before using wireshark, but not sure why the same request receives response on SOAPUI. –  Nov 18 '13 at 09:19
  • @Alex Are you connecting to the server differently? – The Guy with The Hat Nov 18 '13 at 13:05
  • @Alex `SOAPConnection.call()` can return a `SOAPException`. from your code it is not clear how you handle this exception. Could you wrap your call in a try{} catch(e) and tell us if you catch an exception (and what is in it (`e.printStackTrace()`)? – ljgw Nov 21 '13 at 07:27

4 Answers4

2

If you are having NullPointerException at line 50 it's because st is null. That is connection.call(message, endpoint); is returning null.

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:

  • 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

See http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/7-b147/com/sun/xml/internal/messaging/saaj/client/p2p/HttpSOAPConnection.java#HttpSOAPConnection.call%28javax.xml.soap.SOAPMessage%2Cjava.lang.Object%29 Lines 281 to 341.

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.

Dario
  • 548
  • 1
  • 7
  • 14
  • 1
    I've got the first part of your explanation but not the last part, would you give me an example of that, I am not sure what you mean by "download the source code of involved java classes, link ..." –  Nov 23 '13 at 23:08
  • 1
    I was thinking about something like this: http://stackoverflow.com/a/1961628/3008950 applied to the saaj implementation you are using (instead of junit.jar as mentioned in that post)... Usually it can help but honestly I'm afraid this time it's not so straightforward as I expected (I tried that with rt.jar but I'm having issues: the debugger is showing me the source code of the class but it doesn't seem to highlight the line of code it's executing)... but maybe you can figure out how to make it work. – Dario Nov 24 '13 at 20:22
1

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.

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

DYezek
  • 324
  • 1
  • 3
  • 7
  • 1
    but I am using exactly same code for another web service and it works –  Nov 23 '13 at 06:02
0

SOAPConnection.call() is abstract is it not?

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.

Chaffers
  • 176
  • 9
  • I am using the same code for other web service but this one does not work. –  Nov 19 '13 at 10:32
  • Is this a java webservice you are calling or some sort of native code? Either way why not consume the wsdl and generate a client from that? – Chaffers Nov 19 '13 at 16:18
  • Are you absolutely sure your other code doesn't use a HTTPSoapConnection or a WebServicesTempate? The code you are calling is abstract, hence the implementation does not exist, just empty methods. http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/7-b147/javax/xml/soap/SOAPConnection.java#SOAPConnection.call%28javax.xml.soap.SOAPMessage%2Cjava.lang.Object%29 – Chaffers Nov 19 '13 at 16:38
  • 1
    actually I am using the same code for another web service which perfectly works. –  Nov 23 '13 at 06:31
  • Exactly the same code or is there a polymorphic type in there? Something along the lines of SOAPConnection = new HttpSoapConnection, or possibly more likely SOAPConnection is overridden in either your own code or from an import. Hence check the package and type in your imports, is it definitely javax.xml.soap.SOAPConnection, or merely a SOAPConnection object in a different package? – Chaffers Dec 03 '13 at 15:15
0

check whether sm is null by placing system.out.println(" soap message:" +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

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.

rajivas
  • 151
  • 1
  • 6
  • it is really weird, sm is null and I checked the request sent by TCP/IP monitor it is a correct request, as I could receive the response in SoapUI. –  Nov 23 '13 at 06:00
  • If you have access to webservice server Check at server what happens to the request u sent. Verify whether it is processing the request and sending valid response. – rajivas Nov 23 '13 at 06:44
  • I've verified it using SoapUI, I print out the request on console, and copy it in SoapUI which receives a correct response, therefore a correct request will be sent, I've contacted the searver team,they advised me that it can be due to proxy settings but I did not configure any of proxy settings –  Nov 23 '13 at 08:08
  • can you list the SAAJ Jars you are using.. Also let me know if you have SAAJ-Impl jar in classpath. – rajivas Nov 24 '13 at 06:30
  • I've added the content of my pom.xml to the question –  Nov 24 '13 at 08:14