java.io.NotSerializableException

时间:2012-12-15 20:22:41

标签: java exception exception-handling notserializableexception

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

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 个答案:

答案 0 :(得分:175)

您的对象的字段依次是其字段,其中一些字段不实现Serializable。在您的情况下,违规类为TransformGroup。怎么解决?

  • 如果该课程属于您,请将其设为Serializable
  • 如果该类是第三方,但您不需要序列化表单,请将该字段标记为transient
  • 如果您需要其数据及其第三方,请考虑其他序列化方法,例如JSON,XML< BSON,MessagePack等,您可以在不修改其定义的情况下序列化第三方对象。

答案 1 :(得分:63)

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

  

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

     

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

参考:The Serializable Interface

答案 2 :(得分:12)

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

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

答案 3 :(得分:0)

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

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

您可以从这里下载 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;
        }