serialize failing in weird ways

J

James

I'm trying to serialize a simple tree structure - the TreeModel behind
a JTree - but the results aren't what I hoped for (essentially, I'm
getting my default object state, rather than the state I serialized).

Does anyone have any useful pointers? There are no exceptions thrown,
indeed no useful results whatsoever - just a JTree which contains the
wrong hierarchy.


James.
 
J

jan V

James said:
I'm trying to serialize a simple tree structure - the TreeModel behind
a JTree - but the results aren't what I hoped for (essentially, I'm
getting my default object state, rather than the state I serialized).

Does anyone have any useful pointers? There are no exceptions thrown,
indeed no useful results whatsoever - just a JTree which contains the
wrong hierarchy.

Can you post the relevant (short, stripped) code fragment?
 
T

Thomas Fritsch

James said:
I'm trying to serialize a simple tree structure - the TreeModel behind
a JTree - but the results aren't what I hoped for (essentially, I'm
getting my default object state, rather than the state I serialized).

Does anyone have any useful pointers? There are no exceptions thrown,
indeed no useful results whatsoever - just a JTree which contains the
wrong hierarchy.
What kind of TreeModel are you using? I don't know, so I assume you use
DefaultTreeModel.
When looking into the source of DefaultTreeModel, you see it implements
the Serializable interface, and it has the 2 private writeObject and
readObject methods which are called during serialization or
deserialization. So far, so good. But see the actual implementation of
writeObject:
private void writeObject(ObjectOutputStream s) throws IOException {
Vector values = new Vector();

s.defaultWriteObject();
// Save the root, if its Serializable.
if(root != null && root instanceof Serializable) {
values.addElement("root");
values.addElement(root);
}
s.writeObject(values);
}
So everything depends on your tree-model's root-node being serializable.
Otherwise nothing useful is written.
So it comes down to the question: Is your root node serializable?

If you use your own TreeModel implementation instead of
DefaultTreeModel, then you will probably have to implement serialization
by yourself.
 
R

Roedy Green

I'm trying to serialize a simple tree structure - the TreeModel behind
a JTree - but the results aren't what I hoped for (essentially, I'm
getting my default object state, rather than the state I serialized).

For a start examine the file on disk with a hex viewer to see if the
data of interest is in there anywhere.

If not, you have something marked transient you really need to
serialise.

You showed us no code, so I can only guess. Did you call some method
or copy reference to replace the reconstituted tree?
 
R

Roedy Green

Does anyone have any useful pointers? There are no exceptions thrown,
indeed no useful results whatsoever - just a JTree which contains the
wrong hierarchy.

I have question. what happens when you serialise an object that
points to objects that are not serialisable. Does it trigger an
exception? quietly null the references?
 
J

John Currier

Roedy said:
I have question. what happens when you serialise an object that
points to objects that are not serialisable. Does it trigger an
exception? quietly null the references?

Snipped from the javadocs of Serializable:
When traversing a graph, an object may be encountered that does not
support the Serializable interface. In this case the
NotSerializableException will be thrown and will identify the class
of the non-serializable object.

John Currier
http://schemaspy.sourceforge.net
 
J

James

Nothing in my code is marked transient - and I mean *nothing* - and the
data's all there. Indeed, the structure does seem to be reconstituted
correctly when I read it back from file (I examined the data structure
in memory with a debugger) - except JTree doesn't render it sensibly.
Yes, the root of the tree is Serializable.

In fact, given the bizarre misrendering, I suspect the serialization
may be a red herring: the nodes which have children are actually drawn
with folder icons, except when selected.

Stranger still: when I select a node, it's now rendered on top of my
JSplitPane, which suddenly collapses to the left, obliterating my tree.
 
O

Oliver Wong

James said:
Nothing in my code is marked transient - and I mean *nothing* - and the
data's all there. Indeed, the structure does seem to be reconstituted
correctly when I read it back from file (I examined the data structure
in memory with a debugger) - except JTree doesn't render it sensibly.
Yes, the root of the tree is Serializable.

In fact, given the bizarre misrendering, I suspect the serialization
may be a red herring: the nodes which have children are actually drawn
with folder icons, except when selected.

Stranger still: when I select a node, it's now rendered on top of my
JSplitPane, which suddenly collapses to the left, obliterating my tree.

Jan said it best:


jan V said:
Can you post the relevant (short, stripped) code fragment?

- Oliver
 
R

Roedy Green

In fact, given the bizarre misrendering, I suspect the serialization
may be a red herring: the nodes which have children are actually drawn
with folder icons, except when selected.

After your reconstitution you need to fire a event to tell swing to
redraw the entire tree with a TreeModel.fireTreeStructureChanged.
 
R

Roedy Green

Stranger still: when I select a node, it's now rendered on top of my
JSplitPane, which suddenly collapses to the left, obliterating my tree.

Try dumping all the salient detail in ASCII just prior to the save,
and again just after the reconstitution, to see if anything got lost.

You can use a DIFFZILLA style text compare to detect changes.

Perhaps some static data needs to be preserved as well.
 

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
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top