1

I have to pass an ArrayList over tcp socket (Server > Client) over the network. When I pass and Arraylist of Strings it works fine and the client is able accept the ArrayList and and print. but when the ArrayList is off Objects I have many exceptions and it does not work. Have a look below please.

Server

    ArrayList<Database> List = new ArrayList<>(); //Database is a seperate class
    Database item1 = new Database();
            item1.code = "1568";
    item1.name = "Round Table";
    item1.details = "Ikeas best Seller";
    item1.inStock = "17";
    List.add(item1);

...

ObjectOutputStream oout = new ObjectOutputStream(socket.getOutputStream());
oout.writeObject(List);

Client

ObjectInputStream iin =  new ObjectInputStream(socket.getInputStream());
Object items = iin.readObject();

Using this code I have many Exceptions thrown. If I replace the array with a string type array like ArrayList<String> Items = new ArrayList<>(); It will work fine. Any Ideas? I am really stack here. Thank You!

6
  • 1
    Can we see at least one exception out of those many? Commented Feb 17, 2013 at 15:14
  • Does client has Database class in its classpath? Commented Feb 17, 2013 at 15:15
  • 2
    Is Database class serializable? If not, you need to serialize it to send it. Commented Feb 17, 2013 at 15:18
  • Please edit your question to add the stacktraces instead of putting them as comments, thanks. This will make it easier to people wanting to help you. Commented Feb 17, 2013 at 15:19
  • So, I need to add the Serializable library in all three classes I have, client, server, and database right? Commented Feb 17, 2013 at 15:22

1 Answer 1

1

The object you are putting in the list and sending must implement the Serializable interface. That also means that fields within that class will also have to be Serializable.

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

1 Comment

Thank you! I am not advanced in java at all, but after your answer I searched more I solved the problem. I was searching for wrong things, thanks for indicating and for the solution!

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.