Get your CodeRanch badge!*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes RMI - Issues reading the remote object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "RMI - Issues reading the remote object" Watch "RMI - Issues reading the remote object" New topic

RMI - Issues reading the remote object

Edward Gosling

Joined: Jun 16, 2013
Posts: 4
Hi. First time poster here.

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.

String url = "rmi://" + hostName + ":" + port + "/Mediator";
try {
DatabaseFactory factory = (DatabaseFactory) Naming.lookup(url);

return (DB) factory.getClient();

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

DatabaseFactory factory = (DatabaseFactory) myRegistry.lookup("Mediator");

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.

Edward Gosling

Roberto Perillo

Joined: Dec 28, 2007
Posts: 2246

Greetings, Edward! Welcome to JavaRanch!

Well, instead instantiating your DatabaseFactoryImpl class while calling the rebind method, try the following:

On the client side:

Cheers, Bob "John Lennon" Perillo
SCJP, SCWCD, SCJD, SCBCD - Daileon: A Tool for Enabling Domain Annotations
Edward Gosling

Joined: Jun 16, 2013
Posts: 4
Thanks for the welcome .

Sadly no luck fixing my code so far though.

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.

Edward Gosling
Edward Gosling

Joined: Jun 16, 2013
Posts: 4
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.

Thanks for the help Roberto
Roberto Perillo

Joined: Dec 28, 2007
Posts: 2246

Greetings, Edward!

Any news on this issue?
Edward Gosling

Joined: Jun 16, 2013
Posts: 4
Ah, it works now.

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.

Thanks again for the help.
I agree. Here's the link:
subject: RMI - Issues reading the remote object
Similar Threads
Do i need host to setup server?!!
not sure which methods must be synchronized
question about rmi registry
NX: RMI check available port or host number
B&S Stub errors running in network client mode