• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

java.io. NotSerializableException: java.awt.TexturePaint

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ivn Tcakov
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 wish to win the lottery. I wish for a lovely piece of pie. And I wish for a tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic