简体   繁体   中英

Java Serializable failure

I've have a class called Expression that im trying to serialize using the following load()/save() functions. It used to work fine, after making some changes to my code and the Expression class, a call to in.readObject() throws ClassCastException . The error message is: java.lang.ClassCastException: java.lang.String cannot be cast to java.io.ObjectStreamClass

To me, it feels very bad to store binary data as a String. Try to use a more stable way to store the binary output of the ObjectOutputStream. If you really want to stick with a string formatted approach, try to use Base64 encoding, which is always safe.


What I might see as a problem is that you defined the serialUID yourself to a static constant:

private static final long serialVersionUID = 3L;

This UID is used to determine if the classes in the class path are compatible with the class of the input object. So what I suspect is that you modified the class without changing the serialVersionUID. That made the ObjectInputStream think that is is compatible, but you are still working with old data. Try to get rid of the old data, and restart with a fresh save() call.


Are you sure that Input is serializable as well? That the UID of that class isn't corrupt?

it seems serializable does not like NULL values in the fields of the class members. if i make sure that all class members have actual memory it does not fail.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM