So for a programming exercise I have to save data into files. I used the interface Serializable
to save my data.
The problem occurs when I deserialize my file. When I print my object deserialized I can see all the values that have been saved but when I add this object to an arraylist and try to display this object again, all my values are set to default (defined into my superclass).
I don't know if my question is clear but if you need more information please ask.
Here is a part of my code:
public static void loadData(Mouse mouse, Monkey monkey, String fileName) throws IOException, ClassNotFoundException, FileNotFoundException{
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(fileName));
try{
Animal animal = (Animal)ois.readObject();
if (fileName == "Mice.ser"){
animal = (Mouse)animal;
mouse.setLists();
}
else{
animal = (Monkey)animal;
monkey.setLists();
}
}catch(Exception e){}
ois.close();
}
public class Mouse extends Animal implements Serializable{
protected List mouseG1 = new ArrayList();
protected List mouseG2 = new ArrayList();
public void setLists(){
if (this instanceof MouseG1){
mouseG1.add((MouseG1)this);
}
else{
mouseG2.add((MouseG2)this);
}
}
That's it, sorry for my english (and code) I'm a beginner (and french)