Search code examples
javaweb-serviceswebservice-client

Java Web Service Client NullPointerException on calling a Web Method


Anytime I call the Method from the Web Service, the Client gets a NullPointerException.

These are the results I got from testing:

  • The tool "SoapUI" shows that the WebService works and returns the expected values

  • None of the variables is null

  • The returned value from the WebService doesn't seem to reach the client

  • The client successfully connects to the WebService

Exception:

javafx.fxml.LoadException: 
... Not Important ...
Caused by: java.lang.NullPointerException
at com.sun.xml.internal.bind.v2.ClassFactory.create0(Unknown Source)
at com.sun.xml.internal.bind.v2.ClassFactory.create(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.reflect.Lister$CollectionLister.startPacking(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.reflect.Lister$CollectionLister.startPacking(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.reflect.NullSafeAccessor.get(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.reflect.Accessor.getUnadapted(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl$5.get(Unknown Source)
at com.sun.xml.internal.ws.db.glassfish.RawAccessorWrapper.get(Unknown Source)
at com.sun.xml.internal.ws.client.sei.ResponseBuilder$DocLit$PartBuilder.readResponse(Unknown Source)
at com.sun.xml.internal.ws.client.sei.ResponseBuilder$DocLit.readResponse(Unknown Source)
at com.sun.xml.internal.ws.client.sei.StubHandler.readResponse(Unknown Source)
at com.sun.xml.internal.ws.db.DatabindingImpl.deserializeResponse(Unknown Source)
at com.sun.xml.internal.ws.db.DatabindingImpl.deserializeResponse(Unknown Source)
at com.sun.xml.internal.ws.client.sei.SyncMethodHandler.invoke(Unknown Source)
at com.sun.xml.internal.ws.client.sei.SyncMethodHandler.invoke(Unknown Source)
at com.sun.xml.internal.ws.client.sei.SEIStub.invoke(Unknown Source)
at com.sun.proxy.$Proxy40.getOnlineUser(Unknown Source)
at application.Controller.init(Controller.java:141)
at application.Controller.initialize(Controller.java:74)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
... 17 more

My WebService:

    @WebService
    @SOAPBinding(style = javax.jws.soap.SOAPBinding.Style.DOCUMENT, use=javax.jws.soap.SOAPBinding.Use.LITERAL, parameterStyle=javax.jws.soap.SOAPBinding.ParameterStyle.WRAPPED)
    public interface CClientService {
      public ObservableList<Benutzer> getOnlineUser(int id);
    }

My Web Service Impl:

@WebService
public class CClientServiceImpl implements CClientService {
private BenutzerService benutzerService;
private Notification notification;

    @Override
    public ObservableList<Benutzer> getOnlineUser(int id) {
        return benutzerService.getAllOnlineBenutzerWithoutMe(id);
    }
}

My Notification:

public class Notification extends Observable {

@Override
public synchronized void addObserver(Observer o) {
    notifyTheObservers(o);
    super.addObserver(o);
}

@Override
public synchronized void deleteObserver(Observer o) {
    super.deleteObserver(o);
    notifyTheObservers(o);
}

public void notifyTheObservers(Object arg){
    setChanged();
    super.notifyObservers(arg);
}

}

My BenutzerService:

public class BenutzerService extends Repository<Benutzer> {

    public ObservableList<Benutzer> getAllOnlineBenutzerWithoutMe(int id){
       ObservableList<Benutzer> listeAllerBenutzer = FXCollections.observableArrayList();
       listeAllerBenutzer.setAll(em.createNamedQuery("Benutzer.findAllOnlineWithoutMe",Benutzer.class).setParameter("b_id", id).getResultList());
       return listeAllerBenutzer;
    }
}

My Repository:

public abstract class Repository<T> {
   protected EntityManagerFactory emf = Persistence.createEntityManagerFactory("unitname");
   protected EntityManager em = emf.createEntityManager();

   abstract public void create(T object);
   abstract public void update(T object);
   abstract public void delete(T object);
   abstract public T findById(int id);
   abstract public ObservableList<T> getAll();
}

My Client:

public class CClient implements Observer {
private Client client;
private final String host = "localhost";
private final String port = "8080";
private final String path = "MyCoolPath?wsdl";
private CClientService cclientService;
public CClient(){
    client = new Client();
    this.cclientService = getService();
}

public Client getClient() {
    return client;
}

@Override
public void update(Observable o, Object arg) {
    switch(arg.getClass().getSimpleName()){
        case "String":
            System.out.println("Got it, my String.");
            break;
        default:
            System.out.println(arg.getClass().getSimpleName()+"<<<<<<<<");
            break;
    }
}

private CClientService getService(){
    URL urlAdresse = null;
    try {
        urlAdresse = new URL("http://"+ host + ":"+ port + "/" + path);
    } catch (MalformedURLException e) {
        System.out.println("No Connection!");
        e.printStackTrace();
    }

    QName qnameService = new QName("http://webservice/",
            "CClientServiceImplService");
    QName qnamePort = new QName("http://webservice/",
            "CClientServiceImplPort");

    Service service = Service.create(urlAdresse,qnameService);
    CClientService ws = service.getPort(qnamePort,CClientService.class);

    return ws;
}

public CClientService getCclientService() {
    return this.cclientService;
}
}

The Web Service and the Client are in a different Project.


Solution

  • First of all, I changed the Annotation @WebService from my "CClientServiceImpl.class" to @WebService(endpointInterface="webservice.CClientService"), because I realized with an System.out.println(ws.get...().size()); that I get the Error Message "Unkown Port", after I changed the Annotation the "Unknown Port" Error was solved.

    The Problem was, that I want to return an "ObservableList"... I changed it to "List" (at "CClientServiceImpl.class" and "BenutzerService.class") and the Problem was solved... I checked everything, but not to try what happens if I get a normal java.util.List.

    So My WebService Interface Method looks like this:

    public List<Benutzer> getOnlineUser(int id);
    

    and the Implementation like this:

    @Override
    public List<Benutzer> getOnlineUser(int id) {
        return benutzerService.getAllOnlineBenutzerWithoutMe(id);
    }
    

    Finally my Service was changed to this:

    public List<Benutzer> getAllOnlineBenutzerWithoutMe(int id){
        List<Benutzer> listeAllerBenutzer = FXCollections.observableArrayList();
        listeAllerBenutzer = (em.createNamedQuery("Benutzer.findAllOnlineWithoutMe",Benutzer.class).setParameter("b_id", id).getResultList());
        return listeAllerBenutzer;
    }