wood burning stoves
The moose likes Web Services and the fly likes JAX-WS @WebServiceRef injection NullPointerException solution in NetBeans Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Web Services
Bookmark "JAX-WS @WebServiceRef injection NullPointerException solution in NetBeans" Watch "JAX-WS @WebServiceRef injection NullPointerException solution in NetBeans" New topic

JAX-WS @WebServiceRef injection NullPointerException solution in NetBeans

R Srini
Ranch Hand

Joined: Feb 19, 2010
Posts: 215

I get a NullPointerException when attempting to use dependency injection in a web service Client program. I have been trying to figure out this problem for a while now. The article Unmanaged JAX-WS clients do not allow injection of @WebServiceRef or @WebServiceRefs resources on IBM's support website provides a clue.

After searching all over the place for a solution, I could not find it ... until I started reading the Java EE 5 tutorial. Its on Page 488 of the Java EE 5 tutorial PDF. After some guessing, the following works.

This is using NetBeans IDE 6.8 (needless to say, I don't know how to do the same thing in Eclipse)

Project structure looks like this:

HelloWs (Create a project of type Java Web->Web Application)-->Web Services-->HelloWebService

Create the web service project
Create a web service called HelloWebService in the project, and make a note of the service name you provide.

@WebMethod(operationName = "sayHello")
public String sayHello(@WebParam(name = "name") String name) {return "Hello" + name;}

Create a client project

HelloWsClient (Project type is Java EE->Enterprise Application Client)

// note that HelloWs is the project and GreeterWs is the exposed service name
GreeterWs service; // will be injected. note the data type of the variable. its the service name provided in the @WebService annotation

HelloWebService ws = service.getHelloWebServicePort();
System.out.println ("HelloWebService returned: " + ws.sayHello("Saying hello!"));

Make sure that you can see the wsdl. Right-click the client project and select Run File, and it should work.

Hope this is helpful to someone!

Now a question if you are still reading this. I am assuming that we just created a "managed" JAX-WS client. This works because I have Glassfishv3 on my local machine. What if the web service were on a remote server and the local machine running the client did not have Glassfish installed? So there is nothing on the client side to "manage" it. Would the client still work?


Ivan Krizsan
Ranch Hand

Joined: Oct 04, 2006
Posts: 2198
To launch a managed client independent of any IDE, you use the appclient command in the GlassFish distribution: http://docs.sun.com/app/docs/doc/820-7701/appclient-1m?a=view
You only need an Application Client Container, in order for this to work, not the entire GlassFish container. I haven't used a standalone Application Client Container yet, though.
Best wishes!
R Srini
Ranch Hand

Joined: Feb 19, 2010
Posts: 215
Thank you, Ivan, for the pointer to the appclient man page!

I noticed that NetBeans generates two jars for project HelloWsConsumerClient:
a) HelloWsConsumerClient.jar
b) HelloWsConsumerClientClient.jar

I am able to run the client in two ways now from the <netbeans project directory>\dist directory:
1. <glassfishv3 install dir>\glassfish\bin\appclient -jar .\HelloWsConsumerClient.jar
2. <glassfishv3 install dir>\glassfish\bin\appclient -client .\HelloWsConsumerClientClient.jar

Thanks for your help!


I agree. Here's the link: http://aspose.com/file-tools
subject: JAX-WS @WebServiceRef injection NullPointerException solution in NetBeans
It's not a secret anymore!