java.io.NotSerializableException

我有这个例外,我不明白为什么会被抛出,或者我应该如何处理它。

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

其中element是包含其他一些TransformGroups类Atom的一个实例:

 public class Atom extends Group implements Serializable{ float pozX,pozY; Group group= new Group(); Color3f blue = new Color3f(new Color(255)); Color3f black = new Color3f(new Color(0)); Sphere AtSph=new Sphere(); public Atom(final float WEIGHT, final int BOUNDS,final float radius,Color3f color) { AppSetting ap= new AppSetting(color, black); AtSph=new Sphere(radius,1,100,ap); } } 

完整的错误日志:

 java.io.NotSerializableException: javax.media.j3d.TransformGroup at java.io.ObjectOutputStream.writeObject0(Unknown Source) at java.io.ObjectOutputStream.writeObject(Unknown Source) at cls.MolecularBuilder.addAtom(MolecularBuilder.java:511) at cls.MolecularBuilder$Console.HidrogenItemActionPerformed(MolecularBuilder.java:897) at cls.MolecularBuilder$Console$2.actionPerformed(MolecularBuilder.java:746) at javax.swing.AbstractButton.fireActionPerformed(Unknown Source) at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.setPressed(Unknown Source) at javax.swing.AbstractButton.doClick(Unknown Source) at javax.swing.plaf.basic.BasicMenuItemUI.doClick(Unknown Source) at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(Unknown Source) at java.awt.Component.processMouseEvent(Unknown Source) at javax.swing.JComponent.processMouseEvent(Unknown Source) at java.awt.Component.processEvent(Unknown Source) at java.awt.Container.processEvent(Unknown Source) at java.awt.Component.dispatchEventImpl(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Window.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.EventQueue.dispatchEventImpl(Unknown Source) at java.awt.EventQueue.access$200(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue$4.run(Unknown Source) at java.awt.EventQueue$4.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source) 

注意:AppSetting(在Atom类中)只是一个扩展Appearance的自定义类。

你的对象的字段又有他们的字段,其中一些不能实现Serializable 。 在你的情况下,违规类是TransformGroup 。 如何解决?

  • 如果这个类是你的,那就使它可Serializable
  • 如果class级是第三方,但您不需要以序列化forms存在,请将该字段标记为transient
  • 如果你需要它的数据和它的第三方,考虑其他的序列化方式,比如JSON,XML <BSON,MessagePack等,你可以在没有修改定义的情况下获得第三方对象的序列化。

有时在序列化内部类实例时发生“java.io.NotSerializableException”,因为:

“序列化这样的内部类实例将导致其关联的外部类实例的序列化”

强烈build议不要使用 内部类 (即不是静态成员类的嵌套类),包括本地类和匿名类”

参考: 可序列化的接口

通过实现接口java.io.Serializable使类可序列化。

  • java.io.Serializable – 标记接口,它没有任何方法。
  • 标记接口的用途 – 告诉ObjectOutputStream这个对象是一个可序列化的对象。