I'm trying to get RMI implemented, but have run into the a slight problem. Every time I attempt to access my remote object from the client, it throws a null pointer exception.
Not really certain why it occurs, which certainly doesn't help. I'm kind of hoping that someone else will be able to see where I've gone wrong.
I register my object using these two lines of code via the server application. This code seems to work fine, and myRegistry.list() will show the Mediator object.
Registry myRegistry = java.rmi.registry.LocateRegistry.createRegistry(rmiPort);
myRegistry.rebind("Mediator", new DatabaseFactoryImpl(dbLocation));
On the client application, I try to retrieve a reference to this object.
I believe the other classes all function correctly. (I tested it with hardcoded value instead of a remote one, and they worked as one would expect).
Interestingly enough, if I add
Registry myRegistry = java.rmi.registry.LocateRegistry.getRegistry(hostName, Integer.parseInt(port));
and call myRegistry.list(), it will display my "Mediator" object within the registry, however, sadly trying
results in the same error. A null pointer exception the first time I try to use the remote object.
So, in summary, if I understand it correctly.
- The server correctly creates and registers the remote object.
- The client can see the registered remote object, but when the client attempts to retrieve it, something goes wrong, the object (or the string inside the object) loses it's value, and a NullPointerException is thrown when trying to access this.
I forgot to mention that I had been using 'extends UnicastRemoteObject' on my DatabaseFactoryImpl class.
Once I removed that, I get the same NullPointerException.
By setting my dbLocation String (within my DatabaseFactoryImpl class) to public, I've checked that the String on the object is correct, prior to being sent to the client. However, I'm not quite sure how to check the String on the client side. The variable only exists with DatabaseFactoryImpl class, and not the DatabaseFactory interface that the remote object is returned as. If I try to downcast the interface instance, I get a ClassCastException. This also happens if I try to directly cast the remote object as a DatabaseFactoryImpl when I read it from the registry.
Also tried modifying my DatabaseFactoryImpl constructor so it didn't require a String, and created get/set methods to access the String. It was tidier than changing a private variable to public temporarily, but didn't really have any impact. Still got the same error message, and was still unable to check the value of the String on the client application.
That String is essential, and in theory, if it is set correctly, then everything should work.
Joined: Jun 16, 2013
Not sure how to edit posts, so apologies for a 2nd post.
Progress has been made. It's still not working perfectly, but it's had enough progress that I should be able to troubleshoot the rest without too much difficulty.
It had a very strange error for awhile though.
The server would create and register the rmi, and the client would read it correctly, and then go through the process of creating a connection to the database. It would complete this successfully, but then throw a RemoteException when it tried to return it. The error stopped occuring after I messed around with the code for a bit, though I have no idea how I fixed it (or what caused it in the first place).
In any case, it works, so I'm happy.