2

I am attempting to get my program to save the data inputted by a user. Currently, I am getting a:

java.io.NotSerializableException: java.awt.image.BufferedImage error

Now, what I have done is implement the FileWriter in my user interface class, and by examining the text file it appears it is trying to save all of the information about the text boxes and labels that I have implemented on my UI. In my main class that my UI is based off of, there is an ArrayList which holds the objects of my project. I need to serialize these objects but they contain a BufferedImage. I think I have found a way to work around the BufferedImage error, but I do not want the entire UI to be serialized.

So my question is, should I move the serialization method into the class that contains my ArrayList of objects, so that the UI will not be serialized?

tmaxxcar
  • 309
  • 2
  • 15
  • Frankly, you should avoid serialising objects. Serialisation is only meant for the short term (mins) storage of objects, normally for transmission to other JVMs. Instead, you should using something like XML, possibly JAXB – MadProgrammer May 15 '13 at 03:15
  • You can control what you serialize, as implied by @Andrew. You can also use 'transients' as it fits. – rimero May 15 '13 at 03:18
  • @MadProgrammer If I should avoid serializing objects, then how would you suggest I write my information into an xml? and what exactly is JAXB? – tmaxxcar May 15 '13 at 03:35
  • 1
    JAXB is a bindings API to bind object properties to XML nodes. Checkout [this](https://jaxb.java.net/tutorial/) for more details – MadProgrammer May 15 '13 at 04:23

2 Answers2

3

Mark BufferedImage fields with transient keyword which indicates that a field should not be serialized.

class A {
    transient BufferedImage bufferedImage;
    ...

then you can customize serialization by implementing the following methods in class A

private void writeObject(java.io.ObjectOutputStream s) throws java.io.IOException{
    s.defaultWriteObject();
    // extract bytes from bufferedImage and write them
    ...

private void readObject(java.io.ObjectInputStream s) throws java.io.IOException, ClassNotFoundException {
    s.defaultReadObject();
    // read bytes and re-create bufferedImage
    ...
Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
  • So, I make the BufferedImage transient, then could i parse the image into a byte array, and store that? – tmaxxcar May 15 '13 at 03:33
  • Yes, you can, you need to find a good example how to customize serialization – Evgeniy Dorofeev May 15 '13 at 03:36
  • I think i found a good example located here [link](http://stackoverflow.com/questions/15058663/how-to-serialize-an-object-that-includes-bufferedimages) – tmaxxcar May 15 '13 at 03:37
  • I've spent time on adding customization for nothing :). Looks very much like mine though – Evgeniy Dorofeev May 15 '13 at 03:44
  • ok, cool. Im still a little shakey on how the loading of the serialization will populate my objects information. Guess I have a lot of reading ahead of me :/ – tmaxxcar May 15 '13 at 03:48
1

..should I move the serialization method .. so that the UI will not be serialized?

Yes. Only the things that specifically need serializing, should be serialized.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • ok, then I should move it into the class that contains my ArrayList, as I only want to serialize that. – tmaxxcar May 15 '13 at 03:34