Re: Serializing problems. Help appreciated.
AndyW wrote:
I am modelling the universe for a Java game. I have it working with JBDC and
Access tables but I want to make it far more simple (and make it harder for
people to hack the tables and cheat)
I decided to use serialization to hande the Unvirse Object but all I get is
a 38 byte file.
The code you posted was absent all indentation. Use spaces (not
TABs), up to four per indent level, to indent source posted to
Usenet. This will make it easier to read, thus less likely to
discourage people from reading it.
There are multiple problems with the 'SaveGame' class as you posted
it. You assign a 'Universe' instance reference to a member variable,
and likewise you assign the output streams to instance variables, but
there's no visible reason to do that. Based on what you show, the
'Universe' and stream variables should be local to the method.
Which brings up the point that you're doing all the work in the
'SaveGame' constructor. That's a bad practice. Just let the system
generate a default constructor and use a method to do the saving, or
perhaps make the method static. (There are dangers to static methods
that might make one prefer an instance method.) Constructors are for
construction of the object. The constructor you show has no relevance
to construction, only to behavior. Ergo, it should be a method, not a
constructor.
Lothar Kimmeringer wrote:
Since you ignore exception occuring while serializing the Universe,
there most likely is an exception happening. Is something printed
out in STDERR?
Why do you say he ignores exceptions? His code doesn't, and we have
no direct evidence that the programmer does.
OTOH, we have no evidence that the programmer is looking at the stderr
output either. OP, please tell us what the stack trace shows, or at
least whether it shows..
Also, OP, read and study
<http://sscce.org/>
then follow its advice.
BTW. Standard serialization is a very bad way of persisting data
that should last longer than a short time. So as soon as you change
a single thing in the class or use a different compiler you can end
up with objects that can't be deserialized anymore e.g. because the
serialversion UID has changed or new members are added.
Can anybody tell me where I am going wrong? I have checked and the Universe
object is full of data but it does not serialize to a file.
Lothar Kimmeringer wrote:
Check for an exception happening when calling writeObject. Most
likely in one of the nested members you try to serialize a non-
transient member that is not serializable.
This is where an SSCCE can help us help the OP.
--
Lew