1

I have created working sockets, and when I try to send text or numbers, its ok, but when im trying to send my custom class object, i got NullPointerException... Here is some code:

public boolean SendLi(List<Entity> list)
{
    try {
         out.writeObject(list);
    } catch (IOException e) {
        e.printStackTrace();
        System.err.println("Send: Error on OutputStream.write(byte[])");
    }
    return true;
}

public List<Entity> RecvLi()
{
    List<Entity> data;
    data = new ArrayList<Entity>();
      try{
          data = (List<Entity>) in.readObject();
        } catch (IOException e) {
          System.err.println("Send: Error on OutputStream.read(byte[]) - IOException");
          return null;
        } catch (ClassNotFoundException e) {
          System.err.println("Send: Error on OutputStream.read(byte[]) - ClassNotFound");
            e.printStackTrace();
        }
    return data;
}

Actually, that code I made for Lists, but I want to have some simmilar functions to send other objects. Thanks for any replies!

2 Answers 2

1

You need to Serialize your Custom class before sending it through socket.The object that you are sending in

out.writeObject(list);

should be serialized, your class should implement java.io.Serializable interface

Sign up to request clarification or add additional context in comments.

2 Comments

The problem is that the custom objects are serialized, but it doesnt help. I cant serialize ArrayList, so it must be something other...
Instead List<Entity> try to set array Entity[].
1

Code snippets below works with all kind serializable objects for me. Please check.

*IRequest is object to send. *IResponse is object to get.

Client Code:

    Socket clientSocket = new Socket(hostName, portNo);
    clientSocket.setSoTimeout(connectionTimeout);
    try {
        OutputStream outputStream = clientSocket.getOutputStream();
        InputStream inputStream = clientSocket.getInputStream();
        ObjectOutputStream objOutputStream = new ObjectOutputStream(outputStream);
        ObjectInputStream objInputStream = null;

        try {
            objOutputStream.writeObject(request);
            objOutputStream.flush();
            objInputStream = new ObjectInputStream(inputStream);
            res = (IResponse) objInputStream.readObject();
        } finally {
            if (objOutputStream != null) {
                objOutputStream.close();
            }
            if (outputStream != null) {
                outputStream.close();
            }
            if (objInputStream != null) {
                objInputStream.close();
            }
            if (inputStream != null) {
                inputStream.close();
            }
        }
    } finally {
        clientSocket.close();
        clientSocket = null;
    }

Server Code:

        private IMessageProcessor messageProcessor = null;
        private Socket clientSocket = null;
        private Logger logger = null;


        String clientIP = clientSocket.getInetAddress().getHostAddress();
        int clientPortNr = clientSocket.getPort();

        InputStream inputStream = clientSocket.getInputStream();
        OutputStream outputStream = clientSocket.getOutputStream();
        try {
            ObjectInputStream objInputStream = null;
            ObjectOutputStream objOutputStream = null;
            try {
                IRequest request = null;

                objInputStream = new ObjectInputStream(inputStream);
                objOutputStream = new ObjectOutputStream(outputStream);
                request = (IRequest) objInputStream.readObject();

                IResponse response = null;

                try {
                    response = messageProcessor.processMessage(clientIP,
                                                                clientPortNr,
                                                                request,
                                                                logger);
                    objOutputStream.writeObject(response);
                } catch (Exception ex) {
                    objOutputStream.writeObject(ex);
                }

                objOutputStream.flush();
                objOutputStream.reset();

            } finally {
                if (objInputStream != null)
                    objInputStream.close();
                if (objOutputStream != null)
                    objOutputStream.close();
            }
        } finally {
            inputStream.close();
            outputStream.close();
            clientSocket.close();
        }
    } catch (Exception ex) {
        logger.error(ex.getMessage());
    }

1 Comment

I guess there is something wrong on your side because I use it everyday. Maybe first you should try to transfer a simple Integer object. There can not be any problem with it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.