NotSerializableException owing to Container.add

J

JCM

I have a class ("PieChart") that subclasses JComponent (and by
extension, Container). In the constructor for my class, I call
"this.add(mChart.graph)", where mChart is a transient member variable
and mChart.grpah is not serializable. The problem comes when I try to
serialize the PieChart instance: I can't prevent Java from trying to
write mChart.graph to the output stream (and thereby throwing a
NotSerializableException). I've tried over-riding the writeObject
method with a blank one, as well as with one that calls
"this.remove(mChart.graph)", but to no avail: I don't have control
over how the superclass (Container in this case) handles writing
*it*self, and apparantly this happens before my overriding code is
even called.

I'd appreciate any suggestions on how to get around this; I just want
to be able to write the PieChart object to a file without mChart.graph
being written (indeed, mChart itself I can reconstruct trivially from
other arguments).

Thanks!

-Josh
 
J

John C. Bollinger

JCM said:
I have a class ("PieChart") that subclasses JComponent (and by
extension, Container). In the constructor for my class, I call
"this.add(mChart.graph)", where mChart is a transient member variable
and mChart.grpah is not serializable. The problem comes when I try to
serialize the PieChart instance: I can't prevent Java from trying to
write mChart.graph to the output stream (and thereby throwing a
NotSerializableException). I've tried over-riding the writeObject
method with a blank one, as well as with one that calls
"this.remove(mChart.graph)", but to no avail: I don't have control
over how the superclass (Container in this case) handles writing
*it*self, and apparantly this happens before my overriding code is
even called.

I'd appreciate any suggestions on how to get around this; I just want
to be able to write the PieChart object to a file without mChart.graph
being written (indeed, mChart itself I can reconstruct trivially from
other arguments).

Hmmm. If you are adding mChart.graph to a JComponent, then it must have
a declared type that is a subclass of Component. Component is
Serializable, therefore the type of mChart.graph is too. It is a
semantic error if instances of that class cannot be successfully serialized.

Assuming that you have control over the class of mChart.graph and don't
want to actually write all its data to the output stream, you could play
with that class' writeObject method and perhaps its readObject method.
You might even find a use for writeReplace and/or readResolve.

If you don't have control over the class of mChart.graph then the only
alternative I see is to use a subclass or adapter instead. That way you
can exert serialization control.


John Bollinger
(e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top