Solved

NullPointerException and RMI

Posted on 2002-07-15
12
541 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
Comment Utility
 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
Comment Utility
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
Comment Utility
>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
 
LVL 9

Expert Comment

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

Author Comment

by:pouli
Comment Utility
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
Comment Utility
> 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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

Author Comment

by:pouli
Comment Utility
>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
Comment Utility
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
Comment Utility
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
Comment Utility
 No it's all right. I think doronb is worth the points ]:-)
0
 

Author Comment

by:pouli
Comment Utility
I already posted the question mate :-)

0
 
LVL 35

Expert Comment

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

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
countHi2 challenge 7 43
ArrayLists in ArrayLists/HashMaps? 4 38
Eclipse Neon and jdk 1.8.0 11 63
github account with ecipse 1 15
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

762 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now