1

So I have this class called World:

public class World implements Serializable {

    private int dx;
    private int dy;
    private int x;
    private int y;
    private Path path;
    public ImageIcon image;
    public ArrayList<Location> locations;

I was serializing and deserializing it just fine before I added an object to the ArrayList<Location> locations;

Now when I try to run the serialization

try {
    FileOutputStream fos = new FileOutputStream(Main.board.worldpath.toString());
    ObjectOutputStream oos = new ObjectOutputStream(fos);
    oos.writeObject(Main.board.world.image);
    oos.writeObject(Main.board.world);
    oos.close();
}catch (FileNotFoundException ex) {
    ex.printStackTrace();
} catch (IOException ex) {
    ex.printStackTrace();
}

It throws java.io.NotSerializableException: sun.nio.fs.WindowsPath in this line oos.writeObject(Main.board.world);

The class Location is also serializable:

public class Location implements Serializable {
    public int x;
    public int y;
    public String name;
    public String desc;

I'm not sure why it would say that it's not serializable so is there anything special about serializing ArrayLists?

1 Answer 1

2

The problem is the "Path". The Class is not Serializable. You can save "path" as string for example and create a new path on load.

Implement readObject and writeObject for it and make the path transient.

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

2 Comments

Thank you, I changed it to a String and it works now. Odd, I've been loading and saving these worlds with the Path there but it never threw me an exception.
Perhaps it has always been null up until now.

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.