1

I have been looking for an answer to this question but none of others answers worked for me. I am using serialization in android and I serialize one object. Problem is that when I deserializing it I get EOFException and the object won't load. My saving code:

FileOutputStream fileOut = null;
try {
    fileOut = context.openFileOutput("state.ser", Context.MODE_PRIVATE);
    ObjectOutputStream out = new ObjectOutputStream(fileOut);
    out.writeObject(MyObject);
    out.close();
    fileOut.close();
} catch (IOException i) {
    i.printStackTrace();
}

My loading class which throws that exception:

FileInputStream fis;
try {
    fis = context.openFileInput("state.ser");
    ObjectInputStream is = new ObjectInputStream(fis);
    myObject = (MyObject) is.readObject(); //exception thrown from here
    is.close();
} catch (FileNotFoundException e) {
    generateMyObject();
} catch (Exception e) {
    e.printStackTrace();
}

Definition of my object:

public class MyObject implements Serializable {
    private static final long serialVersionUID = 155986L;   
    public MyInnerObject[][] mio = null;
    public Boolean[] unassigned = null; 
}
public class MyInnerObject implements Serializable {
    private static final long serialVersionUID = 1L;    
    public boolean[] legals;
    public int ID;
    public int pos;
}

What am I doing wrong? Thank you for all answers.

EDIT: stack trace

java.io.EOFException
java.io.DataInputStream.readBoolean(DataInputStream.java:69)
java.io.ObjectInputStream.readNewArray(ObjectInputStream.java:1462)
java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:760)
java.io.ObjectInputStream.readObject(ObjectInputStream.java:1981)
java.io.ObjectInputStream.readObject(ObjectInputStream.java:1938)
java.io.ObjectInputStream.readFieldValues(ObjectInputStream.java:1115)
java.io.ObjectInputStream.defaultReadObject(ObjectInputStream.java:455)
java.io.ObjectInputStream.readObjectForClass(ObjectInputStream.java:1347)
java.io.ObjectInputStream.readHierarchy(ObjectInputStream.java:1244)
java.io.ObjectInputStream.readNewObject(ObjectInputStream.java:1833)
java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:762)
java.io.ObjectInputStream.readObject(ObjectInputStream.java:1981)
java.io.ObjectInputStream.readObject(ObjectInputStream.java:1938)
java.io.ObjectInputStream.readNewArray(ObjectInputStream.java:1490)
java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:760)
java.io.ObjectInputStream.readObject(ObjectInputStream.java:1981)
java.io.ObjectInputStream.readObject(ObjectInputStream.java:1938)
java.io.ObjectInputStream.readNewArray(ObjectInputStream.java:1490)
java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:760)
java.io.ObjectInputStream.readObject(ObjectInputStream.java:1981)
java.io.ObjectInputStream.readObject(ObjectInputStream.java:1938)
java.io.ObjectInputStream.readFieldValues(ObjectInputStream.java:1115)
java.io.ObjectInputStream.defaultReadObject(ObjectInputStream.java:455)
java.io.ObjectInputStream.readObjectForClass(ObjectInputStream.java:1347)
java.io.ObjectInputStream.readHierarchy(ObjectInputStream.java:1244)
java.io.ObjectInputStream.readNewObject(ObjectInputStream.java:1833)
java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:762)
java.io.ObjectInputStream.readObject(ObjectInputStream.java:1981)
java.io.ObjectInputStream.readObject(ObjectInputStream.java:1938)
com.example.myApp.Cell.loadState(Cell.java:224)
... rest is only where in app it appeared

Also even that this is only warning, the object doesn't load so I cannot use it.

Rasputin
  • 25
  • 4

2 Answers2

1

I don't know if this can help you, but here it says that serialization in Android can be buggy with large arrays of integers. In your case, you have an array of booleans, which is an integer anyway.

I would try to make tests by stripping some of the serializable data (e.g. serializing only part of the information) to see if the problem is related to the arrays.

Also, this may be obvious, but maybe not... make sure you're not loading a file that has been saved with a different version of your object (e.g. you saved your object doing some test, then you added variables to your class, and now you're trying to de-serialize).

Merlevede
  • 8,140
  • 1
  • 24
  • 39
  • This could be it. If I count all variables that I am saving that takes about 300 000 booleans. I'll try find different representation of state or at least not use integers. Many thanks, I would never think of that. I'll edit when I solve it. – Rasputin Feb 20 '14 at 20:54
  • @user3294807 Something you could try, is replacing the arrays[] for an ArrayList, which adopts the Serializable interface – Merlevede Feb 20 '14 at 21:14
  • I have change my model, so most of the booleans are computed as they are needed and now I am saving only 700 booleans and everything is fine. I really appreciate your help, I wouldn't solve it without you. Thank you – Rasputin Feb 20 '14 at 22:10
0

The file is empty, or short. You must have got an exception when creating it.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • Nope, that is what bothers me. I have rechecked it and also print "info" in the try when saving and the "info" went out (I saw it in console) but no exception. – Rasputin Feb 20 '14 at 20:40