Solved

NullPointerException and RMI

Posted on 2002-07-15
12
545 Views
Last Modified: 2013-12-29
Hi, all
here is my problem. I have a server and 2 clients for testing purposes.

They are communicating through RMI.
When I run them under the same host (without localhost but with real IP address) they run fine.

The clients used to be applications that I tranformed them to applets.

I tested the applications to 3 systems with WinXP both Home and Proffessional Edition

In 1 out of three I took the exception:
 java.lang.NullPointerException
     at com.TavliGame.Interface.Interface.setmessagetext(Interface.java:550)
     at com.TavliGame.Interface.Communicator.settext(Communicator.java:56)
     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
     at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
     at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
     at java.lang.reflect.Method.invoke(Unknown Source)
     at sun.rmi.server.UnicastServerRef.dispatch(Unknown Source)
     at sun.rmi.transport.Transport$1.run(Unknown Source)
     at java.security.AccessController.doPrivileged(Native Method)
     at sun.rmi.transport.Transport.serviceCall(Unknown Source)
     at sun.rmi.transport.tcp.TCPTransport.handleMessages(Unknown Source)
     at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(Unknown Source)
     at java.lang.Thread.run(Unknown Source)
     at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(Unknown Source)
     at sun.rmi.transport.StreamRemoteCall.executeCall(Unknown Source)
     at sun.rmi.server.UnicastRef.invoke(Unknown Source)
     at com.TavliGame.Interface.Communicator_Stub.settext(Unknown Source)
     at com.TavliGame.Brain.game.rollToSeeWhoPlayFirst(Game.java:408)
     at com.TavliGame.Brain.Game.run(Game.java:294)
     at java.lang.Thread.run(Unknown Source)


Now that I have tranformed them to applets I took 2 out of 2 tests this NullPointerException!

In other words in the box that used to run when it was a normal application, now that it is an applet it raises that exception.

I could think that has to do something with the sandbox probably.

Inside the source code this sounds very wierd!
Here is a part of code that the exception is raised:
     public int rollToSeeWhoPlayFirst() throws RemoteException
     {
          System.out.println("1.5 Communicator1 & com2 are " + communicator1 + ", " + communicator2);
          if ( rollAgain == false )
          {
               /*
               *     First of all ask form both players to press teh Start button
               */
               
               System.out.println("2. Communicator1 & com2 are " + communicator1 + ", " + communicator2);
               communicator1.setText("Please press\nthe Start to begin\nthe game");
               communicator2.setText("Please press\nthe Start to begin\nthe game");          
               communicator1.setStart( new Boolean( true ));
               communicator2.setStart( new Boolean( true ));
               communicator1.setTakeItBackEnabled( new Boolean( false ));

At the System.out.println(....)
the server DOES not print NULL but the normal object, and then when it tries to send the message through the stub the exception is raised.

Any ideas about where the error is would be very appreciated ?

0
Comment
Question by:pouli
  • 6
  • 4
  • 2
12 Comments
 
LVL 35

Expert Comment

by:girionis
ID: 7154410
 pouli you still have problems with the RMI stuff ]:-(. You have to bare in mind that indeed Applet sandox security and Applet related stuff are different than other Java applications stuff.

  Is the communicator2 or the communicator1 that is null. Try to split the System.out... statement and see which one fails. Also where is the communicator initialized? Can you psot a small example of your code where the ciommunicator is initialized and is being assigned a value?
0
 
LVL 9

Accepted Solution

by:
doronb earned 50 total points
ID: 7154478
Hi

From reading your exception, I think that NullPointer is encountered in the "com.TavliGame.Interface.Interface" class at the "setmessagetext(..)" in line 550. Can you show us the code of that method?

Doron
0
 

Author Comment

by:pouli
ID: 7154553
>girionis answering:
I am still having problems but different ones. I solved the previous one.

I am thinking that it may be a security problem. The only thing I do inside the program is to communicate with the http://<ip_address>:8080/client/test.html
and
rmi://<ip_address>:2001

I do not see the reason to make it signed applet. I just communicate with the server. Although at a different port to the one which I made the connection. Does this matter ?

At the System.out.println() both of them are Not null.

The communicators are being initialised in the following bit of code in the com.TavliGame.Brain.Game class
     public void run()
     {    
          System.out.println("Inside the run menthod");    
          //Loop until the 2 players(Communicators) have registered themselves
          boolean cont = true;
          while ( cont )
          {
               if ( communicator1 != null && communicator2 != null )
               {
                    cont = false;
               }
               else
               {
                    //Wait a bit
                    pause( 30 );
               }
          }//end of while          
         
          System.out.println("Both clients registered");
         
         
          //wait a bit until the animation is finished at the clients
          try
          {
               Thread.sleep( 2000 );
          }
          catch( Exception e )
          {
               e.printStackTrace();
          }
         
          int who = -1;
          try
          {
               //Roll to see who will play first
               System.out.println("1. Communicator1 & com2 are " + communicator1 + ", " + communicator2);              
               who = rollToSeeWhoPlayFirst();
          }
          catch( RemoteException e )
          {
               e.printStackTrace();


When the clients made the connection with the server through the Naming.lookup and have an object reference (I hope so...) to the Game they invoke the method setPlayer() that sends the object reference to the game.

Now that I am writing these I am thinking that these may not be references but passed by value. Coulf this be ?
If yes how I managed to run them at least once as applications ?

The method setPlayer is:
     public void setPlayer( CommunicatorInterface com, Integer numOfPlayer )
     {
          System.out.println("Server says: Inside the setPlayer method");
          if ( numOfPlayer.intValue() == 1 )
          {    
               communicator1 = com;
               System.out.println("Server says: Communicator1 done");                        
          }
          else
          {
               communicator2 = com;
               System.out.println("Server says: Communicator2 done");                                        
          }
     }


doronb answering:

The part of code that you requested is :
     public void setMessageText( String message )
     {
          panel.setText( message );
     }

Which calls the OperationalPanel object and invokes the following method:

Damn!!!

I think I cauhgt the error!

The problem might be the following:
When the applet/application starts the OperationalPanel object is made with a dealy.

In some machines it may be quick enough and make it before the call to setText, so no NullPointerException is raised

In other now the call is coming to the client before the latter constructs the OperationalPanel object so it raises the NullPointerException.

I will test it and come back to you.
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 9

Expert Comment

by:doronb
ID: 7154579
So, the panel is not created in time to have the setText method invoked on it?
0
 

Author Comment

by:pouli
ID: 7154606
Sometimes. Because I have a number of threads when I nintialise the Interface sometimes the computer makes it but other times not

I added the following code:
     public void setMessageText( String message )
     {
          while( panel == null )
          {
               try
               {
                    Thread.sleep( 500 );
               }
               catch( Exception e )
               {
                    e.printStackTrace();
               }
          }
          panel.setText( message );
     }

and it seems to partially work.

It does not raise the NullPointerException.

What happen now is that it stucks. Probalby I have to execute this code only 1 time, then I will have to ignore this statement.

I will check it again and come back to you
0
 
LVL 35

Expert Comment

by:girionis
ID: 7154797
> Now that I am writing these I am thinking that these may not be references but passed by value. Coulf this be ?

  Although parameters in a single machine are always passed-by-value (both primitives and references) in method invocations in the RMI system it depends whether the value is primitive, object or remote object. If it is a primitive then it is passed-by-value, if it is an object then again it is passed-by-value (but this time it is the object itself not the reference/pointer to the object). In case of remote objects a method returns a local reference to the remote object. (For more info look here: http://developer.java.sun.com/developer/onlineTraining/rmi/RMI.html)

>I added the following code:
>    public void setMessageText( String message )
...
...
...
>and it seems to partially work.

  Why not do a

if (panel != null)
{
     panel.setText( message );
}
0
 

Author Comment

by:pouli
ID: 7154957
>girionis
>Why not do a

>if (panel != null)
>{
>    panel.setText( message );
>}

Because the request will come to the client to set the text but if the panel is NULL then the text will not be set and the request will be simply lost. The server will not send again the request.

So the client will wait until the OperationalPanel object be ready.

What I noticed though is that it takes longer time than I thought too  make the object because of the sleep method.

So I placed the creation of the object higher in the constructor before the communication with the server initialises.

I think we went out of the initial problem.

And because this question should finish I will split the points between both.

I hope you are happy with this both :)

Thank you again for your consideration and your time.

I will post a request to the moderator
0
 

Author Comment

by:pouli
ID: 7156685
Hello, people

Here is the answer form the moderator

>From: kodiakbear  Date: 07/15/2002 03:55PM PST  
>pouli,
>I have reduced the points on the question in half. Go >ahead and accept one of the comments as an answer. Then >create another question in the Java Programming TA for >the other Expert. Title it like Points for "Expert Name", >in the body of the question put for help with and a link >to the origional (20323472).
>Then place a comment in the origional question with what >you have done with a link to the new question so the >experts will be notified.
>
>kb
>Experts Exchange Moderator
 
0
 

Author Comment

by:pouli
ID: 7156690
hmmm,

Should I suppose that the Java Programing TA is simply another question at the Java forum?

girionis I will post a question in order to give you 50 points the title will be "Points for girionis"

OK ?
0
 
LVL 35

Expert Comment

by:girionis
ID: 7156694
 No it's all right. I think doronb is worth the points ]:-)
0
 

Author Comment

by:pouli
ID: 7156698
I already posted the question mate :-)

0
 
LVL 35

Expert Comment

by:girionis
ID: 7156714
 If it so I will accept then. Thank you ]:-)
0

Featured Post

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
topping2 challenge 13 91
oracle 11g 23 83
table example 4 29
How to convert String matching to regex in java 4 33
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question