java.io.NotSerializableException: java.awt.BasicStroke - really confusing

I have a class Box which has two member Size and Location, both Serializable.

Then I have several classes (e.g. Node) which extend Box.

None of all the classes (Box, Size, Location and all extensions of Box e.g. Node) use BasicStroke. I commented out the import of BasicStroke in all classes that are Serializable, so it cannot be used anywhere.

The problem is I use drag and drop, and when Box is also Serializable I get the following error when trying to drop:

java.io.NotSerializableException: java.awt.BasicStroke

at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1081)

at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1375)

at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1347)

at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1290)

at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1079)

at java.io.ObjectOutputStream.writeArray(ObjectOutputStream.java:1251)

at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1075)

at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1375)

at java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:391)

at java.util.Vector.writeObject(Vector.java:1018)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

at java.lang.reflect.Method.invoke(Method.java:585)

at java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:917)

at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1339)

at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1290)

at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1079)

at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1375)

at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1347)

at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1290)

at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1079)

at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:302)

at sun.awt.datatransfer.TransferableProxy.getTransferData(TransferableProxy.java:66)

at java.awt.dnd.DropTargetContext$TransferableProxy.getTransferData(DropTargetContext.java:359)

at kfotobook.kgui.KPageViewer.getFlavorData(KPageViewer.java:767)

at kfotobook.kgui.KPageViewer.getFlavorData(KPageViewer.java:750)

at kfotobook.kgui.KPageViewer.drop(KPageViewer.java:791)

at java.awt.dnd.DropTarget.drop(DropTarget.java:430)

at sun.awt.dnd.SunDropTargetContextPeer.processDropMessage(SunDropTargetContextPeer.java:500)

at sun.awt.dnd.SunDropTargetContextPeer.access$800(SunDropTargetContextPeer.java:53)

at sun.awt.dnd.SunDropTargetContextPeer$EventDispatcher.dispatchDropEvent(SunDropTargetContextPeer.java:812)

at sun.awt.dnd.SunDropTargetContextPeer$EventDispatcher.dispatchEvent(SunDropTargetContextPeer.java:736)

at sun.awt.dnd.SunDropTargetEvent.dispatch(SunDropTargetEvent.java:29)

at java.awt.Component.dispatchEventImpl(Component.java:3826)

at java.awt.Container.dispatchEventImpl(Container.java:2024)

at java.awt.Component.dispatchEvent(Component.java:3803)

at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4212)

at java.awt.LightweightDispatcher.processDropTargetEvent(Container.java:3963)

at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3817)

at java.awt.Container.dispatchEventImpl(Container.java:2010)

at java.awt.Window.dispatchEventImpl(Window.java:1774)

at java.awt.Component.dispatchEvent(Component.java:3803)

at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)

at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242)

at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)

at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)

I do not know how to better describe the error, because I have no clue why BasicStrokes is tried to serialize at all. I am really confused, but perhaps somebody has seen something similar and can give a hint.

Message was edited by:

kudi

[4702 byte] By [kudia] at [2007-9-25]
# 1
As you can see by the stack trace and the heavy recursion in writeObject(), you are writing out something which contains something which contains something ... which contains a Vector of BasicStroke. Probably an AWT or Swing class. Generally speaking you shouldn't serialize GUI objects.
ejpa at 2007-7-14 > top of java,Core,Core APIs...
# 2

Thanks for the hint. Actually I must say once more that I was too tired that I have overseen a memeber field because of bad code layout :-( I better went to sleep...

But there is still something confusing, that is also the reason why I did not search for the fields in the right class.

So I have a class Box and a class Node which extends Box. Node is the one which contains the evil AWT references. When I declare only Node as Serializable, it works. But when I also declare the super class Box as Serializable the error occured.

In my opinion a Serializable sub class with a super class which does not implement Serializable does not make sence, but as an accident I did it. What is the semantic in such a case? (At least I know it could not serialize the sub class as expected...)

Message was edited by:

kudi

kudia at 2007-7-14 > top of java,Core,Core APIs...
# 3

The semantic is that the non-serializable base class won't be serialized but the serializable class will be.

There is always a non-serializable base class, e.g. in the trivial case java.lang.Object. So the situation does make sense.

If making Box serializable fails and making it non-serializable works, clearly Box must have some member that isn't serializable, or that leads to something that isn't serializable.

ejpa at 2007-7-14 > top of java,Core,Core APIs...