This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes I/O and Streams and the fly likes java.io. NotSerializableException: java.awt.TexturePaint Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "java.io. NotSerializableException: java.awt.TexturePaint" Watch "java.io. NotSerializableException: java.awt.TexturePaint" New topic
Author

java.io. NotSerializableException: java.awt.TexturePaint

Ivn Tcakov
Greenhorn

Joined: Oct 15, 2003
Posts: 9
Hello, I am experiencing a new serialization exception. I am not sure if it something subtle that I might have changed in the code or if it is due to a new jdk version, but here it is:
I am trying to save the components of my GUI to the hard disk and I want to make new cloned versions of these components.
To clone, I am using the 'deep copy' example of doubling an object:
static public Object deepCopy(Object oldObj) throws Exception
{
ObjectOutputStream oos = null;
ObjectInputStream ois = null;
try
{
ByteArrayOutputStream bos =
new ByteArrayOutputStream(); // A
oos = new ObjectOutputStream(bos); // B
// serialize and pass the object
oos.writeObject(oldObj); // C
oos.flush(); // D
ByteArrayInputStream bin =
new ByteArrayInputStream(bos.toByteArray()); // E
ois = new ObjectInputStream(bin); // F
// return the new object
return ois.readObject(); // G
}
catch(Exception e)
{
System.out.println("Exception in ObjectCloner = " + e);
throw(e);
}
So when I try to perform this method on a few objects (java.awt.CheckboxGroup, java.awt.Choice
java.awt.List), it gives me an exception about a class that I don't really know where and what it is:
java.io.NotSerializableException: java.awt.TexturePaint
All of the components that I tried to do deepcopy do implement 'Serializeable' Interface so. I don't understand what this exception means.
I would appreciate any comments or suggestions, thanks
Ivan
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
In order for you to be able to serialize an object, the class must implement Serializable (or Externalizable), and all instance fields of the class must either be primitives, be reference types which are also Serializable, or they must be labeled transient (not to be serialized). This rule applies to each of the Serializable objects referenced by a non-transient field. So, if your object has a field referencing another Serializable object which references another Serializable object which references another object which is not Serializable, and none of those fields were transient, then the serialization will fail, as you've seen. The problem you have is that somewhere in your chain of references, you included something that refers to a TexturePaint object, and TexturePaint is not serializable, and since it's a standard library class, you can't edit the code to make it serializable.
So, what to do? Well, to be honest, reconsider whether you really need to serialize your GUI components at all. They're probably going to contain a lot of references to things that you can't serialize. Also they're pretty bulky and inefficient as far as memory usage, and will take a while to serialize properly anyway if you do manage to avoid all the non-serializable stuff. (Though speed and memory may well be unimportant.) So see if you can avoid serializing the GUI at all. Instead, figure out what data you need to save in order to reconstruct the GUI later, and save that. E.g. you could create a GuiData class that has references to whatever data the GUI is displaying. Or a GuiConfig class with info like the user's last preferred size and position for each component, or whatever else you're trying to preserve.
It's possible there will be just one or two parts of the GUI that give you trouble (by being non-Serializable), and you'll be able to serialize most of the GUI with only a little bit of rewriting - but don't count on it. If you want to try anyway and hope for the best, start by locating where the TexturePaint is referenced by a class which you control (where you can edit the source code, that is) and insert a "transient" modifier in front of that field, so it won't be serialized. Try running the program to see if it works at all. Chanes are, the TexturePaint will now be null after deserialization, and you need to figure out how to reconstitute it from other data which has been serialized. Let's see - from the API, it looks like to create a TexturePaint, you need a BufferedImage and a Rectangle2D. Neither of those was serializable either, so you probably need to recreate those too... ugh. It's probable this is going to be too complex to bother with unless you really need to do it. In any event I doubt it's possible to give good general advice here without having access to the code; you're probably on your own. If you realy need to serialize these GUI objects, you'll need to understand how readObject() and writeObject() are used. - consult the API for ObjectOutput and ObjectInput carefully, and maybe find a book with a good section on serialization. Good luck.


"I'm not back." - Bill Harding, Twister
Ivn Tcakov
Greenhorn

Joined: Oct 15, 2003
Posts: 9
Jim, thanks a lot for the response, I really appreciate it. COnsidering that this GUI saving has been giving me headaches from the start, not to mention the time it takes for it to save and then load since it has to go through all the components and all the serializable issues that come with it, I think I will tako your advice since my GUI really has a lot of instances left and right and I can design a pretty simmple class that remmembers just most essential things. I'll look into it, great guide lines though.
Thanks again
Ivan
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: java.io. NotSerializableException: java.awt.TexturePaint
 
Similar Threads
Testing serialization performance
Generic clone/equals Methods
copies of objects
Serialization
Storing serialized objects