2016-09-28 194 views
1

無論何時,我從Web服務調用Method時,客戶端都會收到NullPointerException。Java Web服務客戶端調用Web方法時的NullPointerException

這些都是我從測試得到的結果:

  • 工具「了SoapUI」顯示,WebService的作品,並返回預期值

  • 無變量是null

  • 來自WebService的返回值似乎無法到達客戶端

  • 客戶端成功連接到WebService的

例外:

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 

我的web服務:

@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); 
    } 

我的Web服務默認地將Impl:

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

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

我的通知:

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); 
} 

}

我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; 
    } 
} 

我的倉庫:

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(); 
} 

我的客戶:

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; 
} 
} 

Web服務和客戶端在不同的項目。

+0

我不知道如果它有幫助,但NullPointer在初始化一個像這樣的變量時出現:List tempUserList = 。cclient.getCclientService()getOnlineUser(ID); –

回答

0

首先,我改變了註釋@WebService從我的「CClientServiceImpl.class」到@WebService(endpointInterface="webservice.CClientService"),因爲我有System.out.println(ws.get...().size());我得到的錯誤消息「未知端口」實現後,我改變了註釋「未知端口」錯誤已解決。

問題是,我想返回一個「ObservableList」...我將它更改爲「List」(在「CClientServiceImpl.class」和「BenutzerService.class」)並且問題解決了... I檢查了一切,但不要試圖發生什麼,如果我得到一個正常的java.util.List

所以我的WebService接口方法是這樣的:

public List<Benutzer> getOnlineUser(int id); 

像這樣實施:

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

最後我的服務改變爲這樣:

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; 
}