title: Passing object reference using RMI
body: <p>Hello everyone,</p>
  
  <p>I have been banging my head really hard trying to solve this problem. I really appreciate if anyone can please have a look at my problem and help me.</p>
  
  <p>I have multiple clients that uses Java RMI to establish a connection (register()) and receive a list of connects clients from the server (get()). My goal is to get clients to talk to each other using RMI, without having to register themselves, only the server so far is being register.</p>
  
  <p>I am trying to pass a reference of the client object to the server.</p>
  
  <p>I am getting the following error:</p>
  
  <pre><code>java.rmi.MarshalException: error marshalling arguments; nested exception is: 
      java.io.NotSerializableException: Client
  </code></pre>
  
  <p>I do not want to use Serialization (I believe), as I don't want to pass the object itself, but a reference. Client1 should be able to send a message to Client2 by calling client2.messsage(String username, String message);</p>
  
  <p>I believe I should just show you at this point how I have implemented my code:</p>
  
  <pre><code>public interface RMIClientIntf extends Remote {
      public void message(String name, String message) throws RemoteException;
      public String getName() throws RemoteException;
  }
  </code></pre>
  
  <p>The previous code is what all clients should implement. If the message() function gets called from another client, that means the other class is sending a message.</p>
  
  <p>My client class itself:</p>
  
  <pre><code>public class Client implements RMIClientIntf {
      public Client() throws RemoteException { super(); }
      public String getName() throws RemoteException { }
  }
  </code></pre>
  
  <p>Now, the Main class that creates an instance of client calls the following, to send the remote object to the server:</p>
  
  <pre><code>final Client client = new Client();
  server.register((RMIClientIntf) client, name);
  </code></pre>
  
  <p>On the server side, the register method is defined as:</p>
  
  <pre><code>public interface RMIServerIntf extends Remote {
      public String register(RMIClientIntf cl, String userName) throws RemoteException;
      public RMIClientIntf[] get() throws RemoteException;
  }
  
  
  public class Server extends UnicastRemoteObject implements RMIServerIntf {
      public String register(RMIClientIntf cl, String userName) throws RemoteException {
          ClientStruct newClient = new ClientStruct(cl, userName);
          /* Store the ClientStruct */
          return new String("SUCCESS");
      }
  }
  </code></pre>
  
  <p>After registering, each client will request the list of connected users. The function in the server is the following:</p>
  
  <pre><code>public RMIClientIntf[] get() throws RemoteException {
      RMIClientIntf[] users = new RMIClientIntf[openConnections.size()];
      /* Fill in users from ClientStruct */
      return users;
  }
  </code></pre>
  
  <p>If Client were to also implement Serializable, then the program runs but when client2 calls client1.message(), the message() method gets called for client2 and throws a NullPointer at this point.</p>
  
  <p>I was looking at this link: <a href="http://www.exampledepot.com/egs/java.rmi/args_Args.html" rel="nofollow">http://www.exampledepot.com/egs/java.rmi/args_Args.html</a>, which gave me a hint that it should implement Remote but not Serialization to pass by reference. I am not sure why the program is complaining in my case.</p>
  
  <p>I really appreciate any help I can get. I've been trying to fix this problem for a while and I can't seem to find any solution.</p>
  
  <p>Thank you very much!</p>