Why does writeObject throw java.io.NotSerializableException and how do I fix it?

前端 未结 3 720
自闭症患者
自闭症患者 2020-11-22 12:22

I have this exception and I don\'t understand why it would be thrown or, how I should handle it.

try {
    os.writeObject(element);
} catch (IOException e)          


        
3条回答
  •  囚心锁ツ
    2020-11-22 12:48

    The fields of your object have in turn their fields, some of which do not implement Serializable. In your case the offending class is TransformGroup. How to solve it?

    • if the class is yours, make it Serializable
    • if the class is 3rd party, but you don't need it in the serialized form, mark the field as transient
    • if you need its data and it's third party, consider other means of serialization, like JSON, XML, BSON, MessagePack, etc. where you can get 3rd party objects serialized without modifying their definitions.

提交回复
热议问题