java.io.NotSerializableException

Mih*_*nca 102 java exception-handling exception notserializableexception

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

try {
    os.writeObject(element);
} catch (IOException e) {
    e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)

哪里element是一个TransformGroup包含一些其他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);
    }
}
Run Code Online (Sandbox Code Playgroud)

完整的错误日志:

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)
Run Code Online (Sandbox Code Playgroud)

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

Boz*_*zho 194

对象的字段依次是它们的字段,其中一些字段没有实现Serializable.在你的情况下,违规类是TransformGroup.怎么解决?

  • 如果班级是你的,那就去做吧 Serializable
  • 如果该类是第三方,但您不需要序列化形式,请将该字段标记为 transient
  • 如果您需要其数据及其第三方,请考虑其他序列化方法,如JSON,XML <BSON,MessagePack等,您可以在不修改其定义的情况下序列化第三方对象.

  • 添加到这个优秀的答案:如果你需要它的数据和它的第三方,你可能想要将第三方类包装到你自己的类中,它实现`Serializable`并使用`readObject()`和`writeObject()`来手动序列化第三方类的数据.在某些情况下,这可能是一种合理的方法.http://stackoverflow.com/a/12963580/1208581 (14认同)
  • 好吧,非常感谢,这将是一件非常困难的事情,因为我的对象只作为TransformGroup的一部分存在,并且没有任何变量存储它们.我的应用程序是一个3d分子构建器,我的所有原子和边界都被添加到TransformGroup作为实例 - 例如(new Atom()).问题不仅在于我需要将它们写入文件,而是用户可能想要删除或编辑当前对象.我想我会尝试一些基于XML的序列化,但我对这个概念很陌生,对我来说有点困难.谢谢 (2认同)

Tho*_*Tho 70

有时在序列化内部类实例时会出现"java.io.NotSerializableException",因为:

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

" 内部类的序列化(即,嵌套类不属于静态成员的类),包括本地和匿名类,被 强烈反对 "

参考:可序列化接口

  • 对我来说就是这种情况。我在单元测试中捷径时就发生了这种情况。希望这个答案可以节省一些时间。 (3认同)

Pra*_*kar 13

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

  • java.io.Serializable - 标记接口,其中没有任何方法.
  • 标记接口的目的 - 告诉ObjectOutputStream该对象是可序列化的对象.

  • 你可以阅读问题,它已经实现了Serializable.还有12个人对我很惊讶. (4认同)