Deserialization with socket returns Nullpointer

A

andrewzzz

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
 
K

Knute Johnson

andrewzzz said:
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?
 
G

Gordon Beaton

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top