JAVA JAX-WS NullPointerException at javax.xml.ws.Service.getPort(Service.java:188)
Asked Answered
G

2

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);
Genu answered 1/6, 2013 at 11:22 Comment(5)
Just a quick comment and maybe completely irrelevant to your problem but your QName has an extra /. Is it a typo?Retinue
@SamRad might be relevant. Would explain why service might be nullParasynthesis
There is no extra "/". You write url like "something". In this case there's just no "something". Also the Service is not null. Look at the stacktraceGenu
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.Manoeuvre
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.Stupa
L
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.

Leatherjacket answered 5/7, 2015 at 10:58 Comment(0)
C
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.

Captor answered 20/2, 2018 at 16:32 Comment(2)
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!Pianola
@Pianola 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: jcraftsmen.com/…Bascule

© 2022 - 2024 — McMap. All rights reserved.