我有这个例外,我不明白为什么会抛出它,或者我应该如何处理它。
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的自定义类。
答案 0 :(得分:175)
您的对象的字段依次是其字段,其中一些字段不实现Serializable
。在您的情况下,违规类为TransformGroup
。怎么解决?
Serializable
transient
答案 1 :(得分:63)
序列化内部类实例时,有时会出现“java.io.NotSerializableException”,因为:
“序列化这样的内部类实例将导致序列化 其关联的外部类实例以及“
“内部类的序列化(即,不是的嵌套类) 静态成员类),包括本地和匿名类 强烈反对“
答案 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;
}