【问题标题】:Why does writeObject throw java.io.NotSerializableException and how do I fix it?为什么 writeObject 会抛出 java.io.NotSerializableException,我该如何解决?
【发布时间】:2012-12-03 11:15:30
【问题描述】:

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

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

其中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);
    }
}

完整的错误日志:

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 的自定义类。

【问题讨论】:

  • 貌似javax.media.j3d.TransformGroup本身并没有实现Serializable
  • 为什么Atom 既扩展Group 又拥有Group 成员?

标签: java exception serialization notserializableexception


【解决方案1】:

您的对象的字段又具有它们的字段,其中一些没有实现Serializable。在您的情况下,违规类是TransformGroup。如何解决?

  • 如果课程是你的,请改成Serializable
  • 如果类是第3方,但在序列化形式中不需要,将字段标记为transient
  • 如果您需要它的数据并且它是第三方的,请考虑使用其他序列化方式,如 JSON、XML、BSON、MessagePack 等,您可以在不修改其定义的情况下序列化第三方对象。

【讨论】:

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

java.io.NotSerializableException 可能会在您序列化一个内部类实例时出现,因为:

序列化这样的内部类实例会导致序列化 其关联的外部类实例也是如此

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

参考:The Serializable Interface

【讨论】:

  • 这就是我的情况。当我在单元测试中走捷径时发生了这种情况。希望这个答案可以节省其他人一些时间。
  • 我有一个字段 private final Set 传出 = new TreeSet(new Comparator() { public int compare(ClaimsNode o1, ClaimsNode o2) { return o1.getNativeIndex( ) - o2.getNativeIndex(); } });
  • 太棒了,从字面上看,我在过去的 1 个小时里都沉浸其中。我开始怀疑即使是原始整数也不能序列化,然后我突然想到,这里可能真的有问题。
【解决方案3】:

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

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

【讨论】:

  • 您可以阅读问题,它已经实现了可序列化。仍然 12+ 让我感到惊讶。
【解决方案4】:

如上所述,如果该类是第 3 方类,则需要将其转换为 JSON/XML/BSON 对象,因为第 3 方类对象无法序列化。

我正在处理我的项目并且遇到了同样的问题,我使用 gson 库将我的类对象转换为 JSON 字符串。之后,我在该对象中使用了这个字符串,并通过 ObjectOutputStream 对象传递了它。 在客户端,我使用相同的方法将 JSON 字符串恢复为第 3 方类对象。

您可以从这里下载 jar:https://jar-download.com/artifacts/com.google.code.gson/gson/2.8.2/source-code

我是这样做的:

// Method that returns String through a Object as a parameter i.e to be converted

 public static String  generateJSONStringFromObject(ThirdPartyClass object) throws JsonProcessingException{
            ObjectMapper mapper = new ObjectMapper();
            mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
            String jsonString = mapper.writeValueAsString(object);
            
                return jsonString;
        }

// Method that returns Object through a String as a parameter 

 public static ThirdPartyClass generateObjectFromJSONString(String jsonString){
            Gson gson = new Gson();
            ThirdPartyClass thirdPartyClassObject = gson.fromJson(jsonString, ThirdPartyClass.class);
            return thirdPartyClassObject;
        }

【讨论】:

    猜你喜欢
    • 2018-01-18
    • 2017-10-05
    • 1970-01-01
    • 2013-11-22
    • 2020-09-14
    • 2019-12-22
    • 2017-06-19
    • 2014-04-16
    相关资源
    最近更新 更多