【问题标题】:Object null in my RMI server我的 RMI 服务器中的对象为空
【发布时间】:2014-05-15 14:35:42
【问题描述】:

我在我的 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;

那么为什么我的对象不能在服务器上为空?

请帮忙。

编辑 在我的 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();
        }

    }

我的客户RMI: 我使用多态性来启动类检测我的类:

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”,我可以发送字符串它的工作,但不是我的对象

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

soucr : 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 则不行

【问题讨论】:

    标签: java object null rmi remote-access


    【解决方案1】:

    你的对象不是空的,只有你从中得到的 id 是空的

    System.out.println((CreateFichier)unObject.getId());
    

    如果 unObject 为 null,那么您一定遇到了 NullPointerException。所以检查 getId() 部分。

    【讨论】:

    • 我这样做: CreateFichier val = (dto.metierToInte.CreateFichier)unObject; System.out.println(val.getId()); /// 显示空值
    • 再一次,您的对象不为 null,getNomFichier() 方法返回 null
    • 如果我用 mainTest 在我的服务器上启动我的对象,它可以工作但是如果我在我的 RMI 客户端上启动我的对象,它会返回 null
    • 请为 creerFichier 发布源代码
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-13
    • 2014-09-08
    • 2016-01-06
    • 1970-01-01
    • 1970-01-01
    • 2017-10-01
    相关资源
    最近更新 更多