14

I have simple "HelloWorld" web service deployed on jboss under ubuntu. I have created simple client, but I can't get it to work. I'm getting NullPointerException each time I run the client.

Note that I'm running on Oracle Java 7 under Ubuntu.

Here is the code: HelloWorldClient.java

import java.net.MalformedURLException;
import java.net.URL;

import javax.xml.namespace.QName;
import javax.xml.ws.Service;


public class HelloWorldClient {

public static void main(String[] args){
    URL url;
    try {
        url = new URL("http://localhost:8080/WebServiceProject/helloWorld?wsdl");
        QName qname = new QName("http:///", "HelloWorldImplService");

        Service service = Service.create(url, qname);

        HelloWorld hello = service.getPort(HelloWorld.class);

         System.out.println(hello.sayHello("mkyong"));
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

}

HelloWorld.java

import javax.jws.WebMethod;
import javax.jws.WebService;


@WebService
public interface HelloWorld {

    @WebMethod
    public String sayHello(String name);

}

Stacktrace:

Exception in thread "main" java.lang.NullPointerException
at com.sun.xml.internal.ws.model.RuntimeModeler.getPortTypeName(RuntimeModeler.java:1407)
at com.sun.xml.internal.ws.client.WSServiceDelegate.getPort(WSServiceDelegate.java:334)
at com.sun.xml.internal.ws.client.WSServiceDelegate.getPort(WSServiceDelegate.java:354)
at javax.xml.ws.Service.getPort(Service.java:188)
at HelloWorldClient.main(HelloWorldClient.java:18)

The exception is thrown at this line:

HelloWorld hello = service.getPort(HelloWorld.class);
Bladositto
  • 293
  • 3
  • 14
  • 1
    Just a quick comment and maybe completely irrelevant to your problem but your `QName` has an extra `/`. Is it a typo? – Sam R. Jun 01 '13 at 12:54
  • 1
    @SamRad might be relevant. Would explain why `service` might be null – kolossus Jun 02 '13 at 14:12
  • There is no extra "/". You write url like "http://something/". In this case there's just no "something". Also the Service is not null. Look at the stacktrace – Bladositto Jun 07 '13 at 22:27
  • Are you running your client from a ide? e.g eclipse? If so, make sure the jdk used by jboss is the same used by eclipse. Perhaps one is running oracle jdk 7 (as you mention) but eclipse is running openjdk. – theBittor Jul 10 '15 at 14:23
  • Which JDK do u use? I hit null pointer exception at getPort as well and I solved it by upgrading my JDK from version 1.6 something to 1.7. Hope it will help. – Patrick Wong Aug 09 '16 at 16:40

2 Answers2

4

I have had the same problem myself for a few days now, because the WSDL-file (and service) I was using was moved to a new URL. I finally found the solution here:

http://techtracer.com/2007/08/15/jax-ws-jaxp-tutorial-building-a-stockquote-web-service-client/

In short, everything (should have) started working after I re-generated all the auto-generated java and class files with the following command (on Windows/CygWin)

"C:/Program Files/Java/jdk1.8.0_31/bin/wsimport.exe" -keep  https://domain.com/path_to_wsdl

I had some extra trouble because some old files were left around and clashing with the newly generated ones, but everything slowly started working after I moved all the old files to the recycle bin.

Rune
  • 698
  • 1
  • 7
  • 22
0

It can also happen if your web-service's implementation is different than the interface from your project.

If in your project you have HelloWorld.class declaring some methods that are not present on the web-service side, the getPort(HelloWorld.class) call will raise a null pointer exception.

You can double check the HelloWorld.class interface on your application and the one on the web-service itself to make sure they match.

Romano
  • 445
  • 1
  • 8
  • 20
  • Hi, I think I am having the same problem. I am also getting a NPE. The printed stacktrace includes "javax.xml.ws.Service.getPort(Service.java:119)". How can I "You can double check the HelloWorld.class interface on your application and the one on the web-service itself to make sure they match" ? thanks! – user3441604 Nov 15 '19 at 10:26
  • @user3441604 I ran into same issue as yours. At least from the line number of the exception it seems the same. Solution provided in the link given below worked for me: http://www.jcraftsmen.com/workaround-for-jax-ws-nullpointerexception-when-calling-web-service-from-embedded-java-8-jvm/ – Tahmid Ali Sep 07 '20 at 07:16