7

I've got an object that im reading and writing to and from fileinputstreams/objectinputstreams and objectinputstreams/objectoutputstreams. I keep getting errors that java excpected one serialversionUID, but found another.

In my class I've implemented serializable and have a field like static final long serialVersionUID = 1L; which I thought was enough.

I'm new to java serialization. What am I missing here?

EDIT If it matters, I'm actually trying to write and read an **ArrayList<MyObject>**

Here's the full trace:

java.io.InvalidClassException: com.luxurymode.pojos.Reminder; Incompatible class (SUID): com.luxurymode.pojos.Reminder: static final long serialVersionUID =4209360273818925922L; but expected com.luxurymode.pojos.Reminder: static final long serialVersionUID =1L;
W/System.err( 4504):    at java.io.ObjectInputStream.verifyAndInit(ObjectInputStream.java:2723)
W/System.err( 4504):    at java.io.ObjectInputStream.readNewClassDesc(ObjectInputStream.java:1848)
W/System.err( 4504):    at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:826)
W/System.err( 4504):    at java.io.ObjectInputStream.readNewObject(ObjectInputStream.java:2066)
W/System.err( 4504):    at java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:929)
W/System.err( 4504):    at java.io.ObjectInputStream.readObject(ObjectInputStream.java:2285)
W/System.err( 4504):    at java.io.ObjectInputStream.readObject(ObjectInputStream.java:2240)
W/System.err( 4504):    at java.util.ArrayList.readObject(ArrayList.java:662)
W/System.err( 4504):    at java.lang.reflect.Method.invokeNative(Native Method)
W/System.err( 4504):    at java.lang.reflect.Method.invoke(Method.java:521)
W/System.err( 4504):    at java.io.ObjectInputStream.readObjectForClass(ObjectInputStream.java:1537)
W/System.err( 4504):    at java.io.ObjectInputStream.readHierarchy(ObjectInputStream.java:1460)
W/System.err( 4504):    at java.io.ObjectInputStream.readNewObject(ObjectInputStream.java:2139)
W/System.err( 4504):    at java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:929)
W/System.err( 4504):    at java.io.ObjectInputStream.readObject(ObjectInputStream.java:2285)
W/System.err( 4504):    at java.io.ObjectInputStream.readObject(ObjectInputStream.java:2240)
W/System.err( 4504):    at com.luxurymode.singletons.RemindersSingleton.<init>(RemindersSingleton.java:54)
W/System.err( 4504):    at com.luxurymode.singletons.RemindersSingleton.getInstance(RemindersSingleton.java:66)
W/System.err( 4504):    at com.luxurymode.views.AddReminderView.saveAlarm(AddReminderView.java:290)
W/System.err( 4504):    at com.luxurymode.tab_2.RemindersActivity.onClick(RemindersActivity.java:94)
W/System.err( 4504):    at android.view.View.performClick(View.java:2554)
W/System.err( 4504):    at android.view.View$PerformClick.run(View.java:8962)
W/System.err( 4504):    at android.os.Handler.handleCallback(Handler.java:587)
W/System.err( 4504):    at android.os.Handler.dispatchMessage(Handler.java:92)
W/System.err( 4504):    at android.os.Looper.loop(Looper.java:123)
W/System.err( 4504):    at android.app.ActivityThread.main(ActivityThread.java:4627)
W/System.err( 4504):    at java.lang.reflect.Method.invokeNative(Native Method)
W/System.err( 4504):    at java.lang.reflect.Method.invoke(Method.java:521)
W/System.err( 4504):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
W/System.err( 4504):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
W/System.err( 4504):    at dalvik.system.NativeStart.main(Native Method)
D/AndroidRuntime( 4504): Shutting down VM
Paul
  • 19,704
  • 14
  • 78
  • 96
LuxuryMode
  • 33,401
  • 34
  • 117
  • 188

3 Answers3

8

Are you reading from a file? In that case, it does not matter if you added the serialVersionUID now, it is different from the one stored in the file, and that creates the exception.

A quick solution could be to set serialVersionUID to 4209360273818925922L, which seems to be the serialVersionUID that was automatically generated by java when you saved those object in that file at that time :)

Simone Gianni
  • 11,426
  • 40
  • 49
  • Thanks. I missed that. I tried before to to just set the serialVersionUID to that and it wasn't working for some reason. Works now. Thanks! – LuxuryMode Aug 03 '11 at 00:49
6

As stated in documentation this can happen for three different reasons:

  • The serial version of the class does not match that of the class descriptor read from the stream
  • The class contains unknown datatypes
  • The class does not have an accessible no-arg constructor

So, first of all check that both implementations have the same serialVersionUID. If this is true you have to be sure that the class doesn't use any type that is undefined (or unknown) to the JVM you are trying to deserialize into. Finally you need to provide a standard constructor ClassName() which does empty initialization.

These can be the problems and surely it's one of these, so I don't think you should look forward something weird. From my personal experience I can add also that using different JVM versions to serialize and deserialize can create this problem, so be sure of it too.

Jack
  • 131,802
  • 30
  • 241
  • 343
  • Thank for the prompt reply @Jack. " Finally you need to provide a standard constructor ClassName() which does empty initialization." Could you explain what you mean? Just a regular public ClassName()? I have a constructor like this... – LuxuryMode Aug 03 '11 at 00:35
  • 1
    @Jack. Actually you do not need the `default` constructor for serialization to work. The serialization framework magically instantiates the bit-bucket without calling the default constructor. – Alexander Pogrebnyak Aug 03 '11 at 00:49
  • I'm confused by this "The class contains unknown datatypes" ... what does that mean? it just means the classes in the file have a different structure than was expected? If so, wouldn't this be covered by "The serial version of the class does not match that of the class descriptor read from the stream" ? (because even if not specified, java will create some serial version)... or am I misunderstanding completely and "unknown datatype" means something else? – ycomp Feb 13 '12 at 14:36
  • 1
    It means that the class path in which you are deserializing your data doesn't have a type that is contained in serialized data. This probably occurs because where you seralize you have like `class FooBar ...` that is contained in a class that you send to another JVM in which the same class is not know, so it doesn't know how to deserialize it. – Jack Feb 13 '12 at 15:35
3

You see the first line?
You have differenct serialVersionUIDs, theay don't are compatible with each other, you have to use the same serialVersionUID!

java.io.InvalidClassException:

com.luxurymode.pojos.Reminder; Incompatible class (SUID): com.luxurymode.pojos.Reminder: static final long serialVersionUID =4209360273818925922L; but expected com.luxurymode.pojos.Reminder: static final long serialVersionUID =1L;

timaschew
  • 16,254
  • 6
  • 61
  • 78