Serializing an object is not serializing data members' properties

Hey all,

I've been troubleshooting this issue on IRC for a few hours now, but I'm still having a problem. To sum it up: I've got a Database object, and within there, I've got some Vectors. When I serialize the Database to a file and read it back in, the Vectors are empty (not null references; .size() == 0). I greatly appreciate any help you can give me.

Relevant portion of Database:

publicclass Databaseimplements Serializable{

privatestaticfinallong serialVersionUID = -5923403995467013543;

private Vector<Teacher> teachers;

private GameConfig config;

private Vector<BoardMember> boardMembers;

Source for writing/reading:

public Main(){

data = loadDatabase();

if(data ==null){

System.out.println("Creating a new db");

data =new Database();

data.addTeacher(new Teacher("John Q. Moron"));

System.out.println(data.getTeachers());

if(!saveDatabase()){

System.err.println("Could not save the database!");

}

System.out.println(data.getTeachers());

this.data = loadDatabase();

System.out.println(data.getTeachers());

}

loginGUI =new LoginScreen(this);

loginGUI.launchInterface();

}

/* snip */

publicboolean saveDatabase(){

try{

ObjectOutputStream out =new ObjectOutputStream(new FileOutputStream(SAVE_DIRECTORY+DATABASE_FILENAME));

out.writeObject(data);

out.close();

returntrue;

}catch (Exception e){

e.printStackTrace();

returnfalse;

}

}

private Database loadDatabase(){

Database ret =null;

try{

ObjectInputStream in =new ObjectInputStream(new FileInputStream(SAVE_DIRECTORY+DATABASE_FILENAME));

ret = (Database)(in.readObject());

in.close();

}catch(Exception e){

e.printStackTrace();

}

return ret;

}

And to answer the unasked question: Yes, all the data member objects implement Serializable as well. No Exceptions are thrown when writing or reading (except when a file is not present).

The output from the Main() constructor (when creating a new Database) is:

Creating a new db

[John Q. *****]

[John Q. *****]

[]

[4035 byte] By [kickersnya] at [2007-11-26 16:47:12]
# 1

I had similar problem sometimes ago. It seems that there are problems in restoring data of uninitialized collection.

Try to initialize data as follow:

public class Database implements Serializable{

private static final long serialVersionUID = -5923403995467013543;

private Vector<Teacher> teachers = null;

private GameConfig config = null;

private Vector<BoardMember> boardMembers = null;

topfoxya at 2007-7-8 23:14:39 > top of Java-index,Core,Core APIs...
# 2
That's nonsense. If you don't specify initialisers the default VM behaviour is to initialise all references to null anyway (and all intrinsic types to 0/false) so this change makes no difference.
dannyyatesa at 2007-7-8 23:14:39 > top of Java-index,Core,Core APIs...
# 3
I haven't already undestand the reason, but after such modification all worked fine.
topfoxya at 2007-7-8 23:14:39 > top of Java-index,Core,Core APIs...
# 4
I made the changes with initializing the members to null and it is still serializing to empty Vectors. Any other suggestions?
kickersnya at 2007-7-8 23:14:39 > top of Java-index,Core,Core APIs...
# 5

> That's nonsense. If you don't specify initialisers

> the default VM behaviour is to initialise all

> references to null anyway (and all intrinsic types to

> 0/false) so this change makes no difference.

Exactly, and furthermore the initializers aren't executed when an object is deserialized. He must have fixed something else at the same time.

@OP: can you show us the Data and Teacher classes.

ejpa at 2007-7-8 23:14:39 > top of Java-index,Core,Core APIs...
# 6

OK - try this....

you have

public Main() {

data = loadDatabase();

if(data == null){

but where do you declare the 'data' variable? You set the instance variable 'data' here...

System.out.println(data.getTeachers());

this.data = loadDatabase();

System.out.println(data.getTeachers());

}

Have you got duplicate 'data' declarations? One at class level - uninitialised, one local to Main? Just a guess...

phawdona at 2007-7-8 23:14:39 > top of Java-index,Core,Core APIs...
# 7
actually ignore that - it's a load of nonsense....
phawdona at 2007-7-8 23:14:39 > top of Java-index,Core,Core APIs...