0

I have got a note taking application code in that when I try to read the saved notes it gives me error like this:

 java.io.InvalidClassException: com.slide.adarsh.ezswipe.Note; Incompatible class (SUID): com.slide.adarsh.ezswipe.Note: static final long serialVersionUID =3563451862165282381L; but expected com.slide.adarsh.ezswipe.Note: static final long serialVersionUID =0L;
12-04 18:58:21.854 18205-18205/com.slide.adarsh.ezswipe W/System.err:     at java.io.ObjectInputStream.verifyAndInit(ObjectInputStream.java:2341)
12-04 18:58:21.854 18205-18205/com.slide.adarsh.ezswipe W/System.err:     at java.io.ObjectInputStream.readNewClassDesc(ObjectInputStream.java:1643)
12-04 18:58:21.854 18205-18205/com.slide.adarsh.ezswipe W/System.err:     at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:657)
12-04 18:58:21.854 18205-18205/com.slide.adarsh.ezswipe W/System.err:     at java.io.ObjectInputStream.readNewObject(ObjectInputStream.java:1782)
12-04 18:58:21.854 18205-18205/com.slide.adarsh.ezswipe W/System.err:     at java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:761)
12-04 18:58:21.854 18205-18205/com.slide.adarsh.ezswipe W/System.err:     at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1983)
12-04 18:58:21.854 18205-18205/com.slide.adarsh.ezswipe W/System.err:     at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1940)
12-04 18:58:21.854 18205-18205/com.slide.adarsh.ezswipe W/System.err:     at com.slide.adarsh.ezswipe.Utilities.getAllSavedNotes(Utilities.java:62)
12-04 18:58:21.854 18205-18205/com.slide.adarsh.ezswipe W/System.err:     at com.slide.adarsh.ezswipe.NoteList.onResume(NoteList.java:32)

here is the code:

Utilities.java

public class Utilities {
  public static final String FILE_EXTENSION=".bin";

  public static boolean saveNote(Context context,Note note) {
    String fileName=String.valueOf(note.getDateTime())+FILE_EXTENSION;

    FileOutputStream fos;
    ObjectOutputStream oos;

    try {
        fos=context.openFileOutput(fileName,context.MODE_PRIVATE);
        oos=new ObjectOutputStream(fos);
        oos.writeObject(note);
        oos.close();
        fos.close();

    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
    return true;
  }

  public static ArrayList<Note> getAllSavedNotes(Context context){
    ArrayList<Note> not=new ArrayList<Note>();
    File filesDir=context.getFilesDir();

    ArrayList<String> noteFiles=new ArrayList<String>();

    for (String file : filesDir.list()){
        if (file.endsWith(FILE_EXTENSION)){
            noteFiles.add(file);
        }
    }

    FileInputStream fis;
    ObjectInputStream ois;

    for (int i=0;i<noteFiles.size();i++) {
        try {
            fis=context.openFileInput(noteFiles.get(i));
            ois=new ObjectInputStream(fis);

            not.add((Note) ois.readObject());  //this is the place where exception is raised
            fis.close();
            ois.close();

        } catch (IOException|ClassNotFoundException e) {
            e.printStackTrace();
            return null;
        }
    }

    return not;
  }
}

here is my Note class which implements Serializable:

public class Note implements Serializable  {
  private long mDateTime; //creation time of the note
  private String mTitle; //title of the note
  private String mContent; //content of the note

  public Note(long dateInMillis, String title, String content) {
    mDateTime = dateInMillis;
    mTitle = title;
    mContent = content;
  }

  public void setDateTime(long dateTime) {
    mDateTime = dateTime;
  }

  public void setTitle(String title) {
    mTitle = title;
  }

  public void setcontent(String content) {
    mContent = content;
  }

  public long getDateTime() {
    return mDateTime;
  }


  public String getDateTimeFormatted(Context context) {
    SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss"
            , context.getResources().getConfiguration().locale);
    formatter.setTimeZone(TimeZone.getDefault());
    return formatter.format(new Date(mDateTime));
  }

  public String getTitle() {
    return mTitle;
  }

  public String getContent() {
    return mContent;
  }

}
ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96
Adarsh
  • 165
  • 2
  • 16
  • Possible duplicate of [java.io.invalidClassException during serializing/deserializing](http://stackoverflow.com/questions/6920352/java-io-invalidclassexception-during-serializing-deserializing) – Charuක Dec 04 '16 at 14:06
  • @CharukaSliva i had gone through it but didn't helped out i had even tried setting serialversionUid in Serializable class but it didn't worked – Adarsh Dec 04 '16 at 14:11
  • All the answers given below are perfect, but man you are going to be in serious trouble doing this, and each change in your code will force you to retest with old serialized objects and many more. Why don't you change your approach and try to save the data in sharedPrefernces or sqlite or on some File. – nobalG Dec 04 '16 at 17:09
  • @nobalG Yaa you are right I am now trying a different way for my project – Adarsh Dec 05 '16 at 16:17

2 Answers2

0

This is because you don't add serialVersionUID for your Note pojo.

Each class which implement Serializable need to add serialVersionUID

You can change your Note to:

public class Note implements Serializable {
  private static final long serialVersionUID = 1234567L;
  ...
  ...
}

But you SHOULD NEVER rely on Serializable for persistence of data because each time your data changes, there will likely be some incompatible changes with previous data. Serializable object should be used for temporary usage in my opinion.

ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96
0

Each serialized objects should have serial version uuid. If you don't supply one it will get automatically random for itself.

Each time you build new apk it has chance to change up to your packages.

Since saved objects already one before and you are trying recover them with different uuid is reason why it fails.

Solution 1: Forget your old datas and create new files with supplied uuid. I know it's would sound strange.

Luckily, you can still recover them if you have your previous apk. Check online for decompiling apk and find your class that want to recover and get given random uuid. That's the your recovery key. Add it to your class.

Good luck

Emre Aktürk
  • 3,306
  • 2
  • 18
  • 30