We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Serializable question
Page Index Toggle Pages: 1
Serializable question? (Read 618 times)
Serializable question?
Apr 15th, 2006, 12:02am
 
Hello,

I haven't worked with Serializable objects in Java before on my own, so I'm not sure what I'm doing wrong here in Processing.

I just want an easy way to save data, so I thought I'd just store the whole object. This is code ripped directly from one website I was looking at that gave examples:

 void saveData(){
   try{
     FileOutputStream f_out = new FileOutputStream("mydata.data");
     ObjectOutputStream obj_out = new ObjectOutputStream(f_out);
     obj_out.writeObject(myObject);
     obj_out.close();
     f_out.close();
   }
   catch(Exception e){
     println("Something went wrong(save): " + e);
   }
 }

When this gets run I get this message in the console:
java.io.NotSerializableException: sun.awt.image.ToolkitImage

But I noticed the file still appeared.

When I went to load it though with this code I got another error and nothing happened, the program didn't crash but it didn't have the effect I wanted it to either...

 void loadData(){
   try{
     FileInputStream f_in = new FileInputStream("mydata.data");
     ObjectInputStream obj_in = new ObjectInputStream(f_in);
     Object obj = obj_in.readObject();
     if(obj instanceof MyClass)
       myObject = (MyClass)obj;
   }
   catch(Exception e){
     println("Something went wrong(load): " + e);
   }
 }

java.io.WriteAbortedException: writing aborted; java.io.NotSerializableException: sun.awt.image.ToolkitImage

The MyClass file uses another class of my making which then uses another class of my making. I've made them all serializable. What do I need to do to fix this or is there some other way to go about writing my objects to files?
Re: Serializable question?
Reply #1 - Apr 17th, 2006, 6:39am
 
If no one can help me with this problem, is there any other easy way to store data?
Page Index Toggle Pages: 1