Hind Forsum
Hind Forsum

Reputation: 10527

java sample web service started with "java.lang.NullPointerException", how to fix?

I've got a simple java web service sample program from the internet:

import javax.jws.WebParam;
import javax.jws.WebService;
import javax.xml.ws.Endpoint;
import java.util.Date;

@WebService
interface IService {
    void hello(@WebParam(name="username") String username);
}

@WebService(targetNamespace = "ServiceImpl", endpointInterface="IService")
class ServiceImp implements IService{
    @Override
    public void hello(@WebParam(name = "username") String username) {
        System.out.println("hello " + username + " now is " + new Date());
    }
}

public class ServiceMain {
    public static void main(String[] args) {
        String address = "http://localhost:7777/myService";
        Endpoint.publish(address, new ServiceImp());
        System.out.println("OK");
    }
}

It compiles and runs with exception:

Exception in thread "main" java.lang.NullPointerException
at com.sun.xml.internal.ws.model.RuntimeModeler.getPortTypeName(RuntimeModeler.java:1618)
at com.sun.xml.internal.ws.model.RuntimeModeler.getPortTypeName(RuntimeModeler.java:1584)
at com.sun.xml.internal.ws.server.EndpointFactory.create(EndpointFactory.java:226)
at com.sun.xml.internal.ws.server.EndpointFactory.createEndpoint(EndpointFactory.java:144)
at com.sun.xml.internal.ws.api.server.WSEndpoint.create(WSEndpoint.java:563)
at com.sun.xml.internal.ws.api.server.WSEndpoint.create(WSEndpoint.java:545)
at com.sun.xml.internal.ws.transport.http.server.EndpointImpl.createEndpoint(EndpointImpl.java:308)
at com.sun.xml.internal.ws.transport.http.server.EndpointImpl.publish(EndpointImpl.java:231)
at com.sun.xml.internal.ws.spi.ProviderImpl.createAndPublishEndpoint(ProviderImpl.java:126)
at javax.xml.ws.Endpoint.publish(Endpoint.java:240)
at ServiceMain.main(ServiceMain.java:22)

So where does this code snippet get wrong, and how to fix it? Thanks a lot.

Upvotes: 0

Views: 402

Answers (1)

Arjit
Arjit

Reputation: 425

You have to provide fully qualified name for endpoint interface. Try this if you wanna lose the endpoint interface.

import javax.jws.WebParam;
import javax.jws.WebService;
import javax.xml.ws.Endpoint;
import java.util.Date;


interface IService {
    void hello(String username);
}

@WebService(targetNamespace = "ServiceImpl")
class ServiceImp implements IService{

    public void hello(@WebParam(name = "username") String username) {
         System.out.println("hello " + username + " now is " + new Date());
    }
}

public class ServiceMain {
    public static void main(String[] args) {
        String address = "http://localhost:7777/myService";
        Endpoint.publish(address, new ServiceImp());
        System.out.println("OK");
    }
}

Otherwise, assuming your endpoint interface resides in a package called your.pkg, try this.

package your.pkg;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.xml.ws.Endpoint;
import java.util.Date;

@WebService
interface IService {
    void hello(String username);
}

@WebService(targetNamespace = "ServiceImpl", endpointInterface="your.pkg.IService")
class ServiceImp implements IService{

    public void hello(@WebParam(name = "username") String username) {
         System.out.println("hello " + username + " now is " + new Date());
    }
}

public class ServiceMain {
    public static void main(String[] args) {
        String address = "http://localhost:7777/myService";
        Endpoint.publish(address, new ServiceImp());
        System.out.println("OK");
    }
}

I was able to run it with both approaches and started getting WSDL from endpoint :- http://localhost:7777/myService?wsdl

Upvotes: 1

Related Questions