Win a copy of AWS Security this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

RMI - Issues reading the remote object

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 2292
3
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greetings, Edward! Welcome to JavaRanch!

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



On the client side:

 
Edward Gosling
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 2292
3
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greetings, Edward!

Any news on this issue?
 
Edward Gosling
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Tomorrow is the first day of the new metric calendar. Comfort me tiny ad:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic