NotSerializableException

NotSerializableException

本文介绍了java.io.NotSerializableException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

其中元素一个 TransformGroup 包含一些其他 TransformGroups 类的一个实例Atom:

  public class Atom extends Groupizable Serializable {
float pozX,pozY;
组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 
在java.io.ObjectOutputStream.writeObject0(未知来源)
在java.io.ObjectOutputStream.writeObject(未知源)
在cls.MolecularBuilder.addAtom(MolecularBuilder.java:511)
在cls.MolecularBuilder $ Console.HidrogenItemActionPerformed(MolecularBuilder.java:897)
at cls.MolecularBuilder $ Console $ 2.actionPerformed(MolecularBuilder.java:746)
在javax.swing.AbstractButton.fireActionPerformed(未知来源)
在javax.swing.AbstractButton $ Handler.actionPerformed(未知源)
在javax.swing.DefaultButtonModel.fireActionPerformed(未知源)
在javax.swing.DefaultButtonModel.setPressed(未知来源)
在javax.swing.AbstractButton.doClick(未知来源)
在javax.swing.plaf.basic.BasicMenuItemUI.doClick(未知来源)
a t javax.swing.plaf.basic.BasicMenuItemUI $ Handler.mouseReleased(Unknown Source)
java.awt.Component.processMouseEvent(Unknown Source)
javax.swing.JComponent.processMouseEvent(Unknown Source)
在java.awt.Component.processEvent(未知源)
在java.awt.Container.processEvent(未知源)
在java.awt.Component.dispatchEventImpl(未知源)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
在java.awt.LightweightDispatcher.processMouseEvent(未知源)
在java.awt.LightweightDispatcher.dispatchEvent(未知源)
在java.awt.Container.dispatchEventImpl(未知源)
在java .awt.Window.dispatchEventImpl(未知源)
在java.awt.Component.dispatchEvent(未知源)
在java.awt.EventQueue.dispatchEventImpl(Unknown So urs)
在java.awt.EventQueue.access $ 200(未知源)
在java.awt.EventQueue $ 3.run(未知源)
在java.awt.EventQueue $ 3.run(未知源)
在java.security.AccessController.doPrivileged(本机方法)
在java.security.ProtectionDomain $ 1.doIntersectionPrivilege(未知源)
在java.security.ProtectionDomain $ 1.doIntersectionPrivilege(未知来源)
在java.awt.EventQueue $ 4.run(未知来源)
在java.awt.EventQueue $ 4.run(未知来源)
在java.security.AccessController.doPrivileged( Native方法)
在java.security.ProtectionDomain $ 1.doIntersectionPrivilege(未知源)
在java.awt.EventQueue.dispatchEvent(未知源)
在java.awt.EventDispatchThread.pumpOneEventForFilters(未知来源)
在java.awt.EventDispatchThread.pumpEventsForFilter(未知源)
在java.awt.EventDispatchThread.pumpEventsForHierarchy(未知源)
在java.awt.Ev entDispatchThread.pumpEvents(未知源)
在java.awt.EventDispatchThread.pumpEvents(未知源)
在java.awt.EventDispatchThread.run(未知来源)
注意:AppSetting(在Atom类中)只是一个扩展Appearance的自定义类。

解决方案

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




  • 如果该类是你的,使它可序列化 / li>
  • 如果课程是第三方,但您不需要按序列化的形式,将该字段标记为 transient

  • 如果您需要其数据并且是第三方,请考虑其他方式的序列化,如JSON,XML& BSON,MessagePack等,您可以在不修改其定义的情况下将第三方对象序列化。


I have this exception I do not understand why would be thrown or how should I handle it.

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

Where element is a TransformGroup containing some other TransformGroups an instance of the class 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);
    }
}

The full error log:

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)

Note: AppSetting (in Atom class) is just a custom class that extends Appearance .

解决方案

The fields of your object have in turn their fields, some of which do not implement Serializable. In your case the offending class is TransformGroup. How to solve it?

  • if the class is yours, make it Serializable
  • if the class is 3rd party, but you don't need it in the serialized form, mark the field as transient
  • if you need its data and it's third party, consider other means of serialization, like JSON, XML< BSON, MessagePack, etc. where you can get 3rd party objects serialized without modifying their definitions.

这篇关于java.io.NotSerializableException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 06:47