1

I have a class with a lot of objects like

private class MyDataStuff{

    private String mostInterestingString;
    private int    veryImportantNumber
    //...you get the idea

    public MyDatastuff{
    //init stuff...
    }

    //some getter methods        

}

Furthermore I have a class, lets call it User that has a list of MyDataStuff , some longs, Strings etc. I want to store an object of User into a file on the internal storage. I tried it with following code:

//loading
try{
        FileInputStream fis = this.getApplicationContext().openFileInput("UserData.data");
        ObjectInputStream is = new ObjectInputStream(fis);
        User loadedUser = (User) is.readObject();
        is.close();
        fis.close();
        appUser = loadedUser;
    }catch (Exception e){
        Log.e("MainActivity", "Error: loading from the internal storage failed - \n" + e.toString());

}
//Saving
if(appUser == null){
    Log.e("MainActivity", "Create new User");
    appUser = new User();
    try{
        FileOutputStream fos = this.getApplicationContext().openFileOutput("UserData.data", Context.MODE_PRIVATE);
        ObjectOutputStream os = new ObjectOutputStream(fos);
        os.writeObject(this);
        os.close();
        fos.close();
    }catch (Exception e){
        Log.e("MainActivity", "Error: Failed to save User into internal storage - \n" + e.toString());
    }
}

This leads to an java.io.NotSerializableException. i read the Serializable-documentation and made testwise the class User implement Serializable and deleted every atribute except for longs and Strings. It still leads to this Exception wich makes me believe that Strings or longs aren't serializable by default either.

I need to save the object in its current state. Is there a better way in doing what I am trying to do, and if not, how can I fix this problem?

1
  • You should concentrate on a standard way of saving data. Save the objects to an XML file or into a database. api 21 has a persistable bundle which you could put a parceled object into but I haven't tried it yet. Commented Jan 15, 2015 at 17:37

2 Answers 2

1

Look closely at your serialization code.

//Saving
if(appUser == null){
    Log.e("MainActivity", "Create new User");
    appUser = new User();
    try{
        FileOutputStream fos = this.getApplicationContext()
                .openFileOutput("UserData.data", Context.MODE_PRIVATE);
        ObjectOutputStream os = new ObjectOutputStream(fos);
        os.writeObject(this);
        ...

You're creating a new User object but you're serializing this which I'm guessing is either an Activity or a Fragment. Hence, you're receiving a NotSerializableException.

Strings and Longs can be serialized with no issues. However, if your final User implementation would have a list of MyDataStuff, you would have to mark its class Serializable as well.

Sign up to request clarification or add additional context in comments.

1 Comment

what a stupid mistake, it seems I require more coffee. Thank you very much good sir.
0

You need to write appUser object and not "this" if you want to store the user object.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.