Re: Help java.io.NotSerializableException
Netlopa wrote:
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.
[...]
public static void doSave(Object gestore) {
[...]
Hashtable h = *new* Hashtable();
h.put("gestore.Partita", gestore);
Hashtable implements Serializable, so you can try to write
it to an ObjectOutputStream. But that's not enough: all the
keys and values in the Hashtable must also implement Serializable,
because the serialized form of the Hashtable must include the
serializations of the table's content. Your problem is (probably)
that the object `gestore' is not Serializable -- so when the
ObjectOutputStream tries to write `gestore', it finds that it
cannot and throws an exception.
You can only serialize objects whose classes implement
Serializable, so calling doSave() with a non-Serializable object
will eventually cause trouble. To catch the problem at compile
time instead of at run time, consider changing the method to
public static void doSave(Serializable gestore)
You will also need to implement Serializable in whatever class(es)
`gestore' might refer to.
By the way, Hashtable is considered old-fashioned and most
programmers avoid using it in new code. Consider HashMap instead.
--
Eric.Sosman@sun.com