Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   Deserialization with socket returns Nullpointer (http://www.velocityreviews.com/forums/t390997-deserialization-with-socket-returns-nullpointer.html)

andrewzzz 02-20-2007 08:20 PM

Deserialization with socket returns Nullpointer
 
Hi guys,
I keep on getting nullpointer exc. when I deserialize from the socket
both locally and from a remote pc. Note that the communication beetwen
the classes works well , cause It gives no exceptions and I can print
out successfully the state of the socket.
The classes are 3 :
1 -"Initiator" : the client side which should collect some data , and
serialize and send through socket and "AgentMessage" object(is a
simple container..).
(And this class seems to work from some debug)..


2-"Server" is a simple server which only reiceves the message on the
socket without deserializing (note that the connection is established
well) and passes the reference of th socket to a thread : the 3rd
class.

3."ServerMT" only aims at printing out on screen the content of the
"AgentMessage" object : firstly receives the socket reference from
"Server" class...than should deserialize the socket to an
"AgentMessage" object.....but it doesn't work.... exceptions of
nullpointer are thrown when I try to read any of the fields of the
deserialized AgentMessage.


Here is the most simplifyed code of the classes

Packages :
-> test -> Initiator (I think that works cause it printed out the
message's content...so I won't paste its code...eheh )
-> AgentsLibrary -> Server
-> AgentsLibrary ->ServerMT
-> AgentsLibrary ->AgentMessage


Server

public class Server {

private static int port=1400;
private static Platform pf;
private int msgn=0;


/**
* Public Constructor.
* The constructor only aims at the creation a new instance
* of the Platform class,which will load local platform details.
*/
public Server(Platform platform){

pf=platform;//works without problems...

}
/**
* Start method waits for incoming messages using socket on the
specified TCP port
* and creates and start a ServerMT thread (a service thread) for
each incoming request.
* @return <code>void</code>
*/
public void start() {
try {
ServerSocket serverSocket;
serverSocket = new ServerSocket(port);
InetAddress address = InetAddress.getLocalHost( );

while(true) {

System.out.println("Waiting for incoming messages on port "+port);
Socket socket = serverSocket.accept();
startThread(socket);//method of this class

}

} catch (IOException e) {
System.out.println("Unrecoverable Network Exception");
e.printStackTrace();
}
}

public void startThread(Socket s) {
ServerMT serverThread=new ServerMT(s);
ServerThread.start();
}


}





ServerMT

package agentsLibrary;

import java.io.*;
import java.net.Socket;

public class ServerMT extends Thread {

private Socket socket;
public ServerMT(Socket socket){
this.socket=socket;
}

public void run(){

//Deserialize From Net
AgentMessage am;
try{
ObjectInputStream in = new
ObjectInputStream(socket.getInputStream());
Object myObject = in.readObject();
in.close();

if(myObject.equals(null))throw new IOException();
am=(AgentMessage)myObject;
System.out.println("AgentMessage :"+am.getSp().getAgentName());

} catch (ClassNotFoundException e) {
System.exit(1);

} catch (IOException e) {

System.exit(1);
} catch (NullPointerException e) {//this nullpointer is thrown...

System.exit(1);
}

}

}







I've tryied about everything but unluckily without success...
Please note that I also tried to perform the deserialization in the
startThread(Socket s) method of the Server (without calling the
ServerMT)...but it gave the same err....
Anybody should suggest to me how to do that???
Thanks a lot guys....
BYe


Knute Johnson 02-20-2007 10:37 PM

Re: Deserialization with socket returns Nullpointer
 
andrewzzz wrote:
> Hi guys,
> I keep on getting nullpointer exc. when I deserialize from the socket
> both locally and from a remote pc. Note that the communication beetwen
> the classes works well , cause It gives no exceptions and I can print
> out successfully the state of the socket.
> The classes are 3 :
> 1 -"Initiator" : the client side which should collect some data , and
> serialize and send through socket and "AgentMessage" object(is a
> simple container..).
> (And this class seems to work from some debug)..
>
>
> 2-"Server" is a simple server which only reiceves the message on the
> socket without deserializing (note that the connection is established
> well) and passes the reference of th socket to a thread : the 3rd
> class.
>
> 3."ServerMT" only aims at printing out on screen the content of the
> "AgentMessage" object : firstly receives the socket reference from
> "Server" class...than should deserialize the socket to an
> "AgentMessage" object.....but it doesn't work.... exceptions of
> nullpointer are thrown when I try to read any of the fields of the
> deserialized AgentMessage.
>
>
> Here is the most simplifyed code of the classes
>
> Packages :
> -> test -> Initiator (I think that works cause it printed out the
> message's content...so I won't paste its code...eheh )
> -> AgentsLibrary -> Server
> -> AgentsLibrary ->ServerMT
> -> AgentsLibrary ->AgentMessage
>
>
> Server
>
> public class Server {
>
> private static int port=1400;
> private static Platform pf;
> private int msgn=0;
>
>
> /**
> * Public Constructor.
> * The constructor only aims at the creation a new instance
> * of the Platform class,which will load local platform details.
> */
> public Server(Platform platform){
>
> pf=platform;//works without problems...
>
> }
> /**
> * Start method waits for incoming messages using socket on the
> specified TCP port
> * and creates and start a ServerMT thread (a service thread) for
> each incoming request.
> * @return <code>void</code>
> */
> public void start() {
> try {
> ServerSocket serverSocket;
> serverSocket = new ServerSocket(port);
> InetAddress address = InetAddress.getLocalHost( );
>
> while(true) {
>
> System.out.println("Waiting for incoming messages on port "+port);
> Socket socket = serverSocket.accept();
> startThread(socket);//method of this class
>
> }
>
> } catch (IOException e) {
> System.out.println("Unrecoverable Network Exception");
> e.printStackTrace();
> }
> }
>
> public void startThread(Socket s) {
> ServerMT serverThread=new ServerMT(s);
> ServerThread.start();
> }
>
>
> }
>
> ServerMT
>
> package agentsLibrary;
>
> import java.io.*;
> import java.net.Socket;
>
> public class ServerMT extends Thread {
>
> private Socket socket;
> public ServerMT(Socket socket){
> this.socket=socket;
> }
>
> public void run(){
>
> //Deserialize From Net
> AgentMessage am;
> try{
> ObjectInputStream in = new
> ObjectInputStream(socket.getInputStream());
> Object myObject = in.readObject();
> in.close();
>
> if(myObject.equals(null))throw new IOException();
> am=(AgentMessage)myObject;
> System.out.println("AgentMessage :"+am.getSp().getAgentName());
>
> } catch (ClassNotFoundException e) {
> System.exit(1);
>
> } catch (IOException e) {
>
> System.exit(1);
> } catch (NullPointerException e) {//this nullpointer is thrown...
>
> System.exit(1);
> }
>
> }
>
> }
>
> I've tryied about everything but unluckily without success...
> Please note that I also tried to perform the deserialization in the
> startThread(Socket s) method of the Server (without calling the
> ServerMT)...but it gave the same err....
> Anybody should suggest to me how to do that???
> Thanks a lot guys....
> BYe
>


Which line of code is causing the NPE? Post the code of the class you
are trying to serialize. How do you know that you are really getting a
NPE and not a ClassNotFoundException?

--

Knute Johnson
email s/nospam/knute/

Gordon Beaton 02-21-2007 06:05 PM

Re: Deserialization with socket returns Nullpointer
 
On 20 Feb 2007 12:20:56 -0800, andrewzzz wrote:

> I keep on getting nullpointer exc. when I deserialize from the
> socket


[...]

> if(myObject.equals(null)) throw new IOException();


The above expression can never be true. The correct way to check if a
reference is null is:

if (myObject == null)

> System.out.println("AgentMessage :"+am.getSp().getAgentName());


am.getSp() might return null, which will cause NPE when you try to do
getAgentName() on it.

If the serialized class implements readObject(), NPE might occur there
too.

/gordon

--
[ don't email me support questions or followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e


All times are GMT. The time now is 10:42 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.