Getting NotSerializableException on a serializable object

后端 未结 2 661
猫巷女王i
猫巷女王i 2021-01-20 04:00

Basically, I\'ve written a program that paints shapes onto the screen, and saves each of the shapes into an ArrayList. What I want to do is figure out how to save the ArrayL

2条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-20 04:41

    To avoid NotSerializableException make sure:

    1. your class implements Serializable
    2. all non primitive members implement Serializable (or are transient instead)
    3. if your class is an inner class it's either static or the outer class implements Serializable

    Besides that you also need to define serialVersionUID for every Serializable class. Check all 3 cases above plus:

    1. all Serializable superclasses
    2. if your class is an anonymous class, define it there too

    Note: your code may work without serialVersionUID sometimes but read the last paragraph in Serializable's javadoc to understand why it will be a problem depending on the environment.


    There's a VM option to add details to the exception. It shows the root and nested classes failing to serialize:

    -Dsun.io.serialization.extendedDebugInfo=true
    

提交回复
热议问题