Help java.io.NotSerializableException

N

Netlopa

Hello,

I'm creating a project (a game) for the university ... but I am in trouble with the management of files (for now, the save).

I still have this exception "java.io.NotSerializableException".
I tried to put anything as a parameter ... but nothing.

To give you an example: the class crossfire.Orco contains various attributes and methods inside ... I would not want this to be the problem and then that should put the "primitive types" (like int, String, etc ...) separated.

Do you have an idea?


Thanks in advance



Netlopa

--
Here I post the code I used for save the file

public class File {



/**

* Create a simple Hashtable and serialize it to a file called

* HTExample.ser.

*/

public static void doSave(Object gestore) {



System.out.println();

System.out.println("+------------------------------+");

System.out.println("| doSave Method |");

System.out.println("+------------------------------+");

System.out.println();



Hashtable h = new Hashtable();

h.put("gestore.Partita", gestore);



/*h.put("string", "Oracle / Java Programming");

h.put("int", new Integer(36));

h.put("double", new Double(Math.PI));*/









try {



System.out.println("Creating File/Object output stream...");



FileOutputStream fileOut = new FileOutputStream("D:\\HTExample.ser");

ObjectOutputStream out = new ObjectOutputStream(fileOut);



System.out.println("Writing Hashtable Object...");

out.writeObject(h);



System.out.println("Closing all output streams...\n");

out.close();

fileOut.close();



} catch(FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}
 
I

Ian Shef

I still have this exception "java.io.NotSerializableException".
I tried to put anything as a parameter ... but nothing.
public class File {



/**

* Create a simple Hashtable and serialize it to a file called

* HTExample.ser.

*/

public static void doSave(Object gestore) {



System.out.println();

System.out.println("+------------------------------+");

System.out.println("| doSave Method |");

System.out.println("+------------------------------+");

System.out.println();



Hashtable h = new Hashtable();

h.put("gestore.Partita", gestore);



/*h.put("string", "Oracle / Java Programming");

h.put("int", new Integer(36));

h.put("double", new Double(Math.PI));*/









try {



System.out.println("Creating File/Object output stream...");



FileOutputStream fileOut = new
FileOutputStream("D:\\HTExample.ser");

ObjectOutputStream out = new ObjectOutputStream(fileOut);



System.out.println("Writing Hashtable Object...");

out.writeObject(h);



System.out.println("Closing all output streams...\n");

out.close();

fileOut.close();



} catch(FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

When you serialize an object, all the objects referenced by that object
must also be serialized, and so on recursively.
Hashtable is serializable. Hashtable h references:
various String objects, which are serializable.
Integer(36), which is serializable.
Double(Math.Pi), which is serializable.
Object gestore, which is NOT serializable.
This is why you get a NotSerializableException.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top