Deserialization with socket returns Nullpointer

Discussion in 'Java' started by andrewzzz, Feb 20, 2007.

  1. andrewzzz

    andrewzzz Guest

    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
    andrewzzz, Feb 20, 2007
    #1
    1. Advertising

  2. 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/
    Knute Johnson, Feb 20, 2007
    #2
    1. Advertising

  3. 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
    Gordon Beaton, Feb 21, 2007
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jack
    Replies:
    7
    Views:
    2,553
  2. Jens Mueller
    Replies:
    0
    Views:
    603
    Jens Mueller
    Apr 12, 2005
  3. VitaminB
    Replies:
    1
    Views:
    789
    Oliver Wong
    Apr 26, 2006
  4. Meikel
    Replies:
    0
    Views:
    486
    Meikel
    Jan 17, 2005
  5. FX
    Replies:
    3
    Views:
    299
    Thomas Fritsch
    Sep 12, 2006
Loading...

Share This Page