uvytautas
uvytautas

Reputation: 528

Serializing object that contains ArrayList of objects

public class User  implements  Serializable{

    public String name;
    public String surname;
    public List<Picked> pickedBooks = new ArrayList<>();

    // Code omitted.
}

Then goes class picked:

public class Picked {

    public Book book;
    public int period;
    public int cost;

    // Code omitted.
}

and class Book:

public class Book {

    public String name;
    public String bookTitle;
    public int howMany;

    // Code omitted.
}

So in the main I create new user and serialize it:

User user = new User();
user.setName("John");
user.setSurname("James");
String fileName = "data.bin";
try (ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream(fileName))) {
    os.writeObject(user);
} catch (IOException ex) {
    Logger.getLogger(Library.class.getName()).log(Level.SEVERE, null, ex);
}

Everything works perfectly, but if I do something like that:

Book book = new Book("Dan Brown", "Angels and Demons", 1);
Picked pck = new Picked(book, 20, 2);
user.add(pck);

And then I want to serialize object user, the program crashes. Output i get is:

java.io.NotSerializableException: at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1183) at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:347) at java.util.ArrayList.writeObject(ArrayList.java:742) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) at java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:988) at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1495) at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1431) at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1177) at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1547) at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1508) at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1431) at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1177) at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:347)

Upvotes: 3

Views: 2681

Answers (2)

Zia
Zia

Reputation: 1011

If your object doesn't implement java.io.Serializable this code throw a NotSerializableException, and the same applies to all its non-static and non-transient data members, and so on recursively until closure. Use specified SerialVersionUID is better, so JVM don't calculate it at runtime. Please also implement the Serializable to Picked and Book class.

In case of array or collection, all the objects of array or collection must be serializable. If any object is not serialiizable, serialization will be failed.

if you have inner class inside your class,then an inner class always holds an implicit reference to its outer class, you cannot serialize an inner class unless the outer class is also serializable. Actually it's not even recommended to try;

look in to this reference http://docs.oracle.com/javase/7/docs/platform/serialization/spec/serial-arch.html#4539

Upvotes: 1

Anubian Noob
Anubian Noob

Reputation: 13596

Picked and Book also need to implement Serializable.

Upvotes: 6

Related Questions