Darin Beaudreau Darin Beaudreau - 8 days ago 4
Android Question

Getting a NotSerializableException on a Serializable object?

I'm getting a

NotSerializableException
when running my app that is complaining about my
AbilityScore
class, but as you can see, the class, and all its members, are
Serializable
(even the enum, even though I don't think that's necessary).

Here is my code :

public class AbilityScore implements Serializable {
private Scores type;
public Scores getScoreType() { return type; }

private int value;
public int getScoreValue() { return value; }
public void setScoreValue(int v) { value = v; }
public int getScoreModifier() { return ((int)(value / 2) - 5); }

private boolean proficient;
public boolean isProficient() { return proficient; }
public void setProficient(boolean p) { proficient = p; }

public AbilityScore(Scores t, int v) {
type = t;
value = v;
}

public enum Scores implements Serializable {
STRENGTH, DEXTERITY, CONSTITUTION, INTELLIGENCE, WISDOM, CHARISMA;
}
}


This is the method causing the exception...

public void saveCharacter(Context context) {
String filename = me.getFilename();

FileOutputStream fout = null;
ObjectOutputStream oos = null;
try {
//fout = new FileOutputStream(filename, false);
fout = context.openFileOutput(filename, MODE_PRIVATE);
oos = new ObjectOutputStream(fout);
oos.writeObject(me);
Toast.makeText(context, "Saved!", Toast.LENGTH_SHORT).show();

if(oos != null) oos.close();
} catch(Exception e) {
Log.e(LOG_TAG, "ERROR: Problem saving character to file => " + filename);
Toast.makeText(context, "Error saving character...", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}

public CharacterInfo loadCharacter(Context context, String filename) {
FileInputStream fin = null;
ObjectInputStream ois = null;
CharacterInfo myCharacter = null;
try {
//fin = new FileInputStream(filename);
fin = context.openFileInput(filename);
ois = new ObjectInputStream(fin);
myCharacter = (CharacterInfo) ois.readObject();
Toast.makeText(context, "Loaded!", Toast.LENGTH_SHORT);

if(ois != null) ois.close();
} catch(Exception e) {
Log.e(LOG_TAG, "ERROR: Problem loading character from file => " + filename);
Toast.makeText(context, "Error loading character...", Toast.LENGTH_SHORT);
e.printStackTrace();
}

return myCharacter;
}


Here's the stack trace for one of the files...

11-30 02:45:11.285 27048-27048/? W/System.err: java.io.WriteAbortedException: Read an exception; java.io.NotSerializableException: edu.uml.android.adventurersarchive.character.AbilityScore
11-30 02:45:11.285 27048-27048/? W/System.err: at java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:778)
11-30 02:45:11.285 27048-27048/? W/System.err: at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1983)
11-30 02:45:11.285 27048-27048/? W/System.err: at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1940)
11-30 02:45:11.285 27048-27048/? W/System.err: at java.io.ObjectInputStream.readNewArray(ObjectInputStream.java:1488)
11-30 02:45:11.285 27048-27048/? W/System.err: at java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:759)
11-30 02:45:11.285 27048-27048/? W/System.err: at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1983)
11-30 02:45:11.285 27048-27048/? W/System.err: at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1940)
11-30 02:45:11.285 27048-27048/? W/System.err: at java.io.ObjectInputStream.readFieldValues(ObjectInputStream.java:1113)
11-30 02:45:11.285 27048-27048/? W/System.err: at java.io.ObjectInputStream.defaultReadObject(ObjectInputStream.java:454)
11-30 02:45:11.285 27048-27048/? W/System.err: at java.io.ObjectInputStream.readObjectForClass(ObjectInputStream.java:1345)
11-30 02:45:11.285 27048-27048/? W/System.err: at java.io.ObjectInputStream.readHierarchy(ObjectInputStream.java:1242)
11-30 02:45:11.285 27048-27048/? W/System.err: at java.io.ObjectInputStream.readNewObject(ObjectInputStream.java:1835)
11-30 02:45:11.285 27048-27048/? W/System.err: at java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:761)
11-30 02:45:11.285 27048-27048/? W/System.err: at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1983)
11-30 02:45:11.285 27048-27048/? W/System.err: at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1940)
11-30 02:45:11.285 27048-27048/? W/System.err: at edu.uml.android.adventurersarchive.GlobalState.loadCharacter(GlobalState.java:54)
11-30 02:45:11.285 27048-27048/? W/System.err: at edu.uml.android.adventurersarchive.LoadCharacterActivity.findCharacters(LoadCharacterActivity.java:47)
11-30 02:45:11.285 27048-27048/? W/System.err: at edu.uml.android.adventurersarchive.LoadCharacterActivity.onCreate(LoadCharacterActivity.java:32)
11-30 02:45:11.285 27048-27048/? W/System.err: at android.app.Activity.performCreate(Activity.java:6056)
11-30 02:45:11.285 27048-27048/? W/System.err: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
11-30 02:45:11.285 27048-27048/? W/System.err: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2332)
11-30 02:45:11.285 27048-27048/? W/System.err: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2441)
11-30 02:45:11.285 27048-27048/? W/System.err: at android.app.ActivityThread.access$800(ActivityThread.java:162)
11-30 02:45:11.285 27048-27048/? W/System.err: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1349)
11-30 02:45:11.285 27048-27048/? W/System.err: at android.os.Handler.dispatchMessage(Handler.java:102)
11-30 02:45:11.285 27048-27048/? W/System.err: at android.os.Looper.loop(Looper.java:135)
11-30 02:45:11.285 27048-27048/? W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5431)
11-30 02:45:11.285 27048-27048/? W/System.err: at java.lang.reflect.Method.invoke(Native Method)
11-30 02:45:11.285 27048-27048/? W/System.err: at java.lang.reflect.Method.invoke(Method.java:372)
11-30 02:45:11.285 27048-27048/? W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:914)
11-30 02:45:11.285 27048-27048/? W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:707)
11-30 02:45:11.285 27048-27048/? W/System.err: Caused by: java.io.NotSerializableException: edu.uml.android.adventurersarchive.character.AbilityScore
11-30 02:45:11.285 27048-27048/? W/System.err: at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1344)
11-30 02:45:11.285 27048-27048/? W/System.err: at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1651)
11-30 02:45:11.285 27048-27048/? W/System.err: at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1497)
11-30 02:45:11.285 27048-27048/? W/System.err: at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1461)
11-30 02:45:11.285 27048-27048/? W/System.err: at java.io.ObjectOutputStream.writeNewArray(ObjectOutputStream.java:1185)
11-30 02:45:11.285 27048-27048/? W/System.err: at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1642)
11-30 02:45:11.285 27048-27048/? W/System.err: at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1497)
11-30 02:45:11.285 27048-27048/? W/System.err: at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1461)
11-30 02:45:11.285 27048-27048/? W/System.err: at java.io.ObjectOutputStream.writeFieldValues(ObjectOutputStream.java:959)
11-30 02:45:11.285 27048-27048/? W/System.err: at java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:360)
11-30 02:45:11.285 27048-27048/? W/System.err: at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1054)
11-30 02:45:11.285 27048-27048/? W/System.err: at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1384)
11-30 02:45:11.285 27048-27048/? W/System.err: at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1651)
11-30 02:45:11.285 27048-27048/? W/System.err: at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1497)
11-30 02:45:11.285 27048-27048/? W/System.err: at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1461)
11-30 02:45:11.285 27048-27048/? W/System.err: at edu.uml.android.adventurersarchive.GlobalState.saveCharacter(GlobalState.java:35)
11-30 02:45:11.285 27048-27048/? W/System.err: at edu.uml.android.adventurersarchive.CreateCharacterActivity.submitCharacter(CreateCharacterActivity.java:73)
11-30 02:45:11.285 27048-27048/? W/System.err: at java.lang.reflect.Method.invoke(Native Method)
11-30 02:45:11.285 27048-27048/? W/System.err: at java.lang.reflect.Method.invoke(Method.java:372)
11-30 02:45:11.285 27048-27048/? W/System.err: at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:284)
11-30 02:45:11.285 27048-27048/? W/System.err: at android.view.View.performClick(View.java:4791)
11-30 02:45:11.285 27048-27048/? W/System.err: at android.view.View$PerformClick.run(View.java:19991)
11-30 02:45:11.285 27048-27048/? W/System.err: at android.os.Handler.handleCallback(Handler.java:739)
11-30 02:45:11.285 27048-27048/? W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
11-30 02:45:11.285 27048-27048/? W/System.err: ... 6 more


And yes,
CharacterInfo
and every member inside is
Serializable
. It's specifically complaining about
AbilityScore
though. What's going on here?

EJP EJP
Answer

The stack trace shows that you must have got a NotSerializableException when writing the object, before you had added implements Serializable to its definition.

You need to delete the serialized file and start again.