0

When I write an object with an ArrayList in the constructor's arguments (even if I do nothing with it) to a file and read it back, it will throw an InvalidClassException and say the serialVersionUID for the objects don't match, however taking it out from the constructor will allow it to behave normally again. What could be causing this error?

This constructor would work fine

public Post(String poster, String postName, int imageWidth, int imageHeight){
    this.poster = poster;
    this.postID = generateID();
    this.postName = postName;
    this.imageWidth = imageWidth;
    this.imageHeight = imageHeight;
}

however this one would throw an error

public Post(String poster, String postName, ArrayList keys, int imageWidth, int imageHeight){
    this.poster = poster;
    this.postID = generateID();
    this.postName = postName;
    this.imageWidth = imageWidth;
    this.imageHeight = imageHeight;
}

The way I'm saving it to a file is just with an ObjectOutputStream with a FileOutputStream as the parameter, and reading it with an ObjectInputStream with a FileInputStream

Here's the stack trace

11-27 15:34:50  java.io.InvalidClassException: net.gladiatorlife.aoit.test.Post; local class incompatible: stream classdesc serialVersionUID = -4349899324072111291, local class serialVersionUID = 5190858650678115513
11-27 15:34:50      at java.io.ObjectStreamClass.initNonProxy(ObjectStreamClass.java:617)
11-27 15:34:50      at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1622)
11-27 15:34:50      at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1517)
11-27 15:34:50      at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1771)
11-27 15:34:50      at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1350)
11-27 15:34:50      at java.io.ObjectInputStream.readObject(ObjectInputStream.java:370)
11-27 15:34:50      at net.gladiatorlife.aoit.test.util.FileUtil.readFromFile(FileUtil.java:48)
11-27 15:34:50      at net.gladiatorlife.aoit.test.order.request.LatestOrder.handle(LatestOrder.java:45)
11-27 15:34:50      at net.gladiatorlife.aoit.test.Connection$1.run(Connection.java:121)
11-27 15:34:50      at java.lang.Thread.run(Thread.java:745)

EDIT Problem solved by manually setting the serializable id to -4349899324072111291L

Adrian Veliz
  • 124
  • 1
  • 7

0 Answers0