125

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

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

哪里elementTransformGroup包含其他一些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 的自定义类。

4

4 回答 4

235

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

  • 如果这门课是你的,那就去做吧Serializable
  • 如果该类是第 3 方,但您不需要序列化形式,则将该字段标记为transient
  • 如果您需要它的数据并且它是第三方的,请考虑其他序列化方式,例如 JSON、XML、BSON、MessagePack等,您可以在不修改其定义的情况下序列化第 3 方对象。
于 2012-12-15T20:26:14.717 回答
86

java.io.NotSerializableException序列化内部类实例时可能会发生,因为:

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

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

参考:可序列化接口

于 2015-01-22T03:37:25.697 回答
10

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

  • java.io.Serializable- 没有任何方法的标记接口。
  • 标记接口的目的 - 告诉ObjectOutputStream这个对象是一个可序列化的对象。
于 2014-01-07T12:28:35.247 回答
1

如上所述,如果该类是第 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;
        }

于 2021-03-19T12:53:44.433 回答