1 year ago

#375593

test-img

Wendell Best

How do I fix a NotSerializable exception

I have a program called Agent that launches a thread called BeaconSender:

    public class BeaconSender extends Thread {

    private Beacon beacon;
    private String managerAddress;
    public BeaconSender(Beacon b, String host){beacon = new Beacon(b); managerAddress = 
    host;}

    public void run(){
        while(true){
            try{
                BeaconListener access = (BeaconListener) 
                Naming.lookup("rmi://"+managerAddress+":1900"+"/BeaconListener");
                access.deposit(beacon);
                System.err.println("Sent beacon to manager");
            }catch(Exception e){
                System.err.println("BeaconSender exception :" + e.toString());
                e.printStackTrace();
            }
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

Which is basically sending a Beacon object periodically to a BeaconListener which is supposed to take the beacon and add it's information to a list of Beacons in a class called Manager.

And the Beacon Class is:

    public class Beacon {
    private int ID; //Randomly generated at start up
    private LocalTime startUpTime; //When this client starts
    private String cmdAgentID; //The registry string of CmdAgent
    private String agentAddress; //The address of the agent

    Beacon(Beacon b){
        this.ID = b.ID;
        this.startUpTime = b.startUpTime;
        this.cmdAgentID = b.cmdAgentID;
        this.agentAddress = b.agentAddress;
    }
    Beacon(String cmdAgentID, String agentAddress){
        Random random = new Random();
        int low = 1024;
        int high = 65535;
        ID = random.nextInt(high-low)+low;
        startUpTime = LocalTime.now();
        this.cmdAgentID = cmdAgentID;
        this.agentAddress = agentAddress;
    }

    public int getBeaconID(){return ID;}
    public void setBeaconID(int idnum){ID = idnum;}
    public void setStartUpTime(LocalTime newTime){this.startUpTime = newTime;}
    public LocalTime getStartUpTime(){return startUpTime;}
    public void setCmdAgentID(String agent){this.cmdAgentID = agent;}
    public String getCmdAgentID(){return cmdAgentID;}
    public String getHost() {return agentAddress;}
}

And the errors I am getting are:

BeaconSender exception :java.rmi.MarshalException: error marshalling arguments; nested exception is: 
    java.io.NotSerializableException: agent.Beacon
    java.rmi.MarshalException: error marshalling arguments; nested exception is: 
    java.io.NotSerializableException: agent.Beacon
    at java.rmi/sun.rmi.server.UnicastRef.invoke(UnicastRef.java:160)
    at java.rmi/java.rmi.server.RemoteObjectInvocationHandler.invokeRemoteMethod(RemoteObjectInvocationHandler.java:217)
    at java.rmi/java.rmi.server.RemoteObjectInvocationHandler.invoke(RemoteObjectInvocationHandler.java:162)
    at com.sun.proxy.$Proxy1.deposit(Unknown Source)
    at agent.BeaconSender.run(BeaconSender.java:19)
Caused by: java.io.NotSerializableException: agent.Beacon
    at java.base/java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1185)
    at java.base/java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:349)
    at java.rmi/sun.rmi.server.UnicastRef.marshalValue(UnicastRef.java:293)
    at java.rmi/sun.rmi.server.UnicastRef.invoke(UnicastRef.java:155)
    ... 4 more

I tried implementing Serializable in each class separately and then both and I still kept getting errors. I am not really sure what is causing java to kick these errors out when "access.deposit(beacon)". I understand serializable has something to do with version control, but I am not sure if my program is in a framework that works with the Serializable interface or I am just missing something in the Manager and the Agent that is keeping me from getting this to work.

java

serialization

rmi

0 Answers

Your Answer

Accepted video resources