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. *****]
[]

