Java.io.NotSerializableException

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

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

Where element is a TransformGroup containing some other TransformGroups instance of the Atom class:

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

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 the Atom class) is just a custom class that extends the look.

+60
java exception exception-handling notserializableexception
Dec 15 '12 at 20:22
source share
3 answers

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

  • If the class belongs to you, make it Serializable
  • If the class is third-party but not needed in serialized form, mark this field as transient
  • if you need its data, and a third party, consider other serialization methods, such as JSON, XML <BSON, MessagePack, etc., where you can get third-party objects serialized without changing their definitions.
+133
Dec 15 '12 at 20:26
source share

Sometimes a "java.io.NotSerializableException" occurs when you serialize an instance of an inner class , because:

"serializing such an internal instance of a class will serialize the associated instance of the external class.

" Serialization of inner classes (i.e. nested classes that are not static member classes), including local and anonymous classes, is greatly discouraged "

Link: Serializable Interface

+44
Jan 22 '15 at 3:37
source share

Make it a serializable class by implementing the java.io.Serializable interface.

  • java.io.Serializable - A token interface that has no methods in it.
  • The purpose of the marker interface is to tell ObjectOutputStream that this object is a serializable object.
+11
Jan 07 '14 at 12:28
source share



All Articles