1

I am getting a NotSerializableException while running an application file trying to write an object to a .dat file.

    try{
        Person[] personList = new Person[3];
        personList[0] = new TeamLeader(8, "Mike Johnson", 29);
        personList[1] = new Engineer(0.4,"Russell Sanders", 22);
        personList[2] = new Engineer(0.7, "Steven Peterson", 25);
        out = new ObjectOutputStream(new FileOutputStream("persons.dat"));
        for(int i = 0; i < personList.length; i++) {
            out.writeObject(personList[i]);//error line 85
        }
        //out.writeObject(personList);
    }

    catch(IOException e) {
        System.out.println("Problem writing file");
        e.printStackTrace();
    } 

error

java.io.NotSerializableException: TeamLeader
     at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1180)
     at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java: 346)
     at CharlieBrownP6.main(CharlieBrownP6.java: 85)

error goes on to about writing aborted for the same error

1
  • 3
    Do Person, TeamLeader, and Engineer implement Serializable? If they don't, then that's your problem - all objects that you serialize need to implement Serializable. Commented Apr 13, 2013 at 4:49

1 Answer 1

2

The objects calling writeObject must implement the Serializable interface (reference).

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

1 Comment

For some reason it was escaping me that Serializable was an interface, once i realized that it made alot more sense. Thanks again stack overflow solved my error

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.