Loading...
Logo
Processing Forum
Hey

I found this old post from "drdoe" at the old forum (published four years ago) and was wondering the same thing - apart from the Boids part. Anyone know if you can serialize in a way that makes the RegisteredMethods become serialized or skipped or something that makes me serialize my own class? Is the question obsolete? Am I missing something obvious?

I was thinking about why it becomes a problem and the current guess is that as long as I have any methods from PApplet in my class, the class will not serialize at all. (?)

It is not a major issue for me even if it would be a wonderfully convenient (and data safe) way to save some data.
I've got a sketch with a some class instances I'd like to export (basically, it's an evolution-simulation with a population of boids). All the involved classes implement Serializable, and java.io.* is imported for the entire sketch.
However, I get the following error:

java.io.NotSerializableException: processing.core.PApplet$RegisteredMethods

(I got a different notSerializable-exception when I used OpenGL, but OpenGL isn't really necessary in this situation [neither is P3D]).
I've got the following code (all in seperate try-catch blocks):

f_out= new FileOutputStream("winner.data");
ObjectOutputStream obj_out = new ObjectOutputStream (f_out);
obj_out.writeObject ( boid[0] ); // the exception above is thrown here

the boid[0]-instance is the boid with the best fitness after x generations, and that class instances some other classes of my own making, but as I mentioned, they all implement Serializable.

Soooo what do I do?
Anyone got any insights in this?

All the best
/JohanW

http://visualinformation.org

Replies(4)

If you don't want to (or cannot) serialize an object (methods has nothing to do with that), you add transient to its declaration. It is better to add it to objects/values that are computed from others, etc.
A good example is the pa (or other name) field holding a reference to the current PApplet.
IIRC, you also need a constructor without parameters, which is called by the deserialization code to build the object. This constructor should initialize the transient objects.
Excellent. I will give it a shot.
I read the part concerning the blank constructor somewhere, but the transient part might just do the trick.
Thanks! I'll try to get back with conclusions.

/JohanW
Oh well. I still get errors.
I do it this way:

transient ClassToSaveInstanceOfToDisk mySerObj; // serialized

...

writing data this way:
           
//             Use a FileOutputStream to send data to a file
//             called myobject.data.
            FileOutputStream f_out = null;
            try {
                f_out = new FileOutputStream ("myobject.data");
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            // Use an ObjectOutputStream to send object data to the
            // FileOutputStream for writing to disk.
            ObjectOutputStream obj_out = null;
            try {
                obj_out = new ObjectOutputStream (f_out);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            // Pass our object to the ObjectOutputStream's
            // writeObject() method to cause it to be written out
            // to disk.
            try {
                obj_out.writeObject(mySerObj);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }


And the error looks like this if interesting...


java.io.NotSerializableException: processing.core.PApplet$RegisteredMethods
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1156)
    at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1509)
    at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1474)
    at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1392)
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1150)
    at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1509)
    at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1474)
    at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1392)
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1150)
    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:326)
    at java.util.HashMap.writeObject(HashMap.java:1000)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:945)
    at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1461)
    at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1392)
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1150)
    at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1509)
    at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1474)
    at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1392)
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1150)
    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:326)
    at slidewalker.SlideWalkerAppTwo$Util.saveFile(SlideWalkerAppTwo.java:3533)
    at slidewalker.SlideWalkerAppTwo.mousePressed(SlideWalkerAppTwo.java:498)
    at processing.core.PApplet.handleMouseEvent(PApplet.java:1776)
    at processing.core.PApplet.dequeueMouseEvents(PApplet.java:1713)
    at processing.core.PApplet.handleDraw(PApplet.java:1605)
    at processing.core.PApplet.run(PApplet.java:1496)
    at java.lang.Thread.run(Thread.java:637)

---------

I think I will go XML anyway...

/JohanW
Please, re-read my answer...
transient modifier is to add to objects you don't want to serialize!
Inside the class you want to serialize.