我在RMI上发送对象,但是,我发现一个空值。
Client.java:
service.InterfaceMathML stub = (service.InterfaceMathML) Naming.lookup("rmi://localhost:1099/BK");
System.out.println("id : " +this.MTI_creerFichier.getId()); // Show 10
int ValRetour = stub.Create(this.MTI_creerFichier, this.MTI_creerFichier.getId(), "CreateFichier");
我的界面:
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface InterfaceMathML extends Remote{
public int Create(Object unObject,String user, String unType)throws RemoteException;
}
我的服务器:
public int Create(Object unObject, String user, String unType) throws RemoteException {
// TODO Auto-generated method stub
switch(unType)
{
case "CreateFichier" :
System.out.println("Create Fichier");
System.out.println((CreateFichier)unObject.getId()); // show Null
ystem.out.println(user); // show userTest
FichierImpl = new JDBCDAO_Fichier();
this.validation = FichierImpl.Validation_ADD((CreateFichier)unObject, user);
if ( this.validation == 1) { this.entier = FichierImpl.add((CreateFichier)unObject, user); }
else { this.entier = this.validation; }
break;
那么为什么我的对象在服务器上不能为null?
请寻求帮助。
编辑
在我的MAINTestServer.java中(在我的serverRMI上)
od = new MathMLServiceImpl() CreateFichier fi = new CreateFichier();
fi.setLogin("test"); fi.setDossierParent("dossierparenttest");
fi.setNomFichier("nomfichiertest");
fi.setContenuFichier("essaie");
int val = od.Create(fi, "test", "CreateFichier"); System.out.println(val);
这是工作
我的RMI服务器
public static void main(String[] args) {
// TODO Auto-generated method stub
String url = "rmi://localhost:1099/BK";
try {
LocateRegistry.createRegistry(1099);
MathMLServiceImpl od = new MathMLServiceImpl();
// je met a dispo un objet dans un url
Naming.rebind("rmi://localhost:1099/BK", od);
System.out.println("Service pret");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
我的CLientRMI:
我使用多态来启动类来检测我的类:
Class<?> actionClass = Class.forName("metierToInte." + nomDto + "Action");
System.out.println(actionClass);
Constructor<?> actionConstructe = actionClass.getConstructor(dto.getClass()); //dto.getClass()
System.out.println("constructeur : " + actionConstructe.toString());
ActionInteToMetier uneActione = (ActionInteToMetier) actionConstructe.newInstance(dto);
System.out.println("action : " + uneActione.toString());
//System.out.println("action : " + uneActione.toString());
uneActione.executer();
它的工作,例如我的类“ CreateFichierAction”,我可以发送String它的工作,但不是我的对象
this.MTI_creerFichier = creerFichier;
service.InterfaceMathML stub = (service.InterfaceMathML) Naming.lookup("rmi://localhost:1099/BK");
System.out.println("id : " +this.MTI_creerFichier.getId()); // Show 10
int ValRetour = stub.Create(this.MTI_creerFichier, this.MTI_creerFichier.getLogin(), "CreateFichier");
System.out.println("valeur retour rmi " + ValRetour);
来源:CreerFichier
package dto.metierToInte;
import java.io.Serializable;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "")
@XmlRootElement(name = "CreateFichier")
public class CreateFichier
extends Fichier
implements Serializable
{
private static final long serialVersionUID = 1L;
@XmlAttribute(name = "id")
protected int id;
public String getId() {
return id;
}
}
可以,但是,如果我的RMI CLIENT不可以
最佳答案
您的对象不为null,只有从中获取的id为null
System.out.println((CreateFichier)unObject.getId());
如果unObject为null,则必须具有NullPointerException。因此,检查getId()部分。
关于java - 我的RMI服务器中的对象为null,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23681373/