2

I have this class:

public class SaveState implements Serializable {
    private static final long serialVersionUID = 1L;
    public List<String> downloadedMagazinesIds = new ArrayList<String>();//id's de revistas descargadas
    public List<String> downloadedMagazinesSummaries = new ArrayList<String>();//página sumario de cada magazine
    public List<FullMagazine> downloadedMagazines = new ArrayList<FullMagazine>();//todos los magazines bajados
    public Magazines ms=null; //Esta clase contiene el array de previews de revistas generado con el parser de XML
}

And I use these methods to store the class (SaveState object) in the sdcard and to read the object from the file into an object again:

public static void saveData(){
    ObjectOutput out;
    try {
        //primero comprobamos si existe el directorio, y si no, lo creamos.
        File folder = new File(Environment.getExternalStorageDirectory() + "/C/");
        if(!folder.exists())
            folder.mkdirs();

        File outFile = new File(Environment.getExternalStorageDirectory(), "/C/appSaveState.data");
        out = new ObjectOutputStream(new FileOutputStream(outFile)); 
        out.writeObject(saveState);
        out.close();
    } catch (Exception e) {e.printStackTrace();}
}


public static void loadData(){
    ObjectInput in;
    try {
        File inFile = new File(Environment.getExternalStorageDirectory(), "/C/appSaveState.data");
        in = new ObjectInputStream(new FileInputStream(inFile));        
        saveState=(SaveState) in.readObject();
        in.close();
    } catch (Exception e) {e.printStackTrace();}
}

I store the object and each time my app is closed and I read the object from the sdcard each time my app is opened and it works fine. The problem comes when I created a newer version of my app (1.01). When I start the app and I try to read the file from the sdcard the file is read but all the variables of the object class are empty, and they are really not empty.

Why doesn't my code work properly? What should I do to fix it?

user229044
  • 232,980
  • 40
  • 330
  • 338
NullPointerException
  • 36,107
  • 79
  • 222
  • 382
  • Firstly, get rid of your horrible exception handling - you should almost never catch `IOException`, and it's not clear to me that `e.printStackTrace()` will go to the relevant logs - have you checked where it *does* go? Also, your `close` calls should be within `finally` blocks. – Jon Skeet Jun 04 '12 at 07:46
  • 1
    i'm not getting any exception, i'm just getting and empty object – NullPointerException Jun 04 '12 at 10:00
  • Would you definitely *notice* if you had an exception, given your exception handling? Can you produce a short but complete program demonstrating the problem? – Jon Skeet Jun 04 '12 at 10:03
  • Jon Skeet, you mean "remove the Exception". Yes, I agree. Its horrid coding practice to catch Exception. – Siddharth Jun 04 '12 at 11:59
  • I HAVE EXACTLY THE SAME PROBLEM. When I created a new version for Production, it reads old objects, but they all empty (nulls & zeros in integer fields etc). I was able to retrieve the file (stream), it's actually not empty. I didn't change this object between app versions, (I checked and double checked), but still.. the file serialized by old version can't be read in the new one. serialVersionUID is set and it's the same (otherwise it would be exception). – Mike Keskinov May 21 '18 at 17:24
  • I just noticed that it was asked in 2012, now 2018 I and run into the same strange issue.... – Mike Keskinov May 21 '18 at 17:30
  • Found the solution; https://stackoverflow.com/questions/19674993/objectinputstream-read-a-empty-object/50456011#50456011 – Mike Keskinov May 21 '18 at 20:15

1 Answers1

1

I have attempted to edit your question to make it more readable and understandable. Hopefully it will help you get some good answers.

I suggest the following

  1. Try to read it using the same version 1.0. This will eliminate any "version" / manifest related issues.
  2. Your FullMagazine may be the root cause, remove that object and try it again.

Let me know what you find. It is a interesting question.

Siddharth
  • 9,349
  • 16
  • 86
  • 148
  • 1
    if i try to read it using version 1.0 i don't have problems to read it, i receive the correct object. I dont understand what you mean with your point 2. If i remove the object obviously i can't read it. – NullPointerException Jun 04 '12 at 10:02
  • If you remove FullMagazine object you will be able to "debug by elimination" and find the root cause :). – Siddharth Jun 05 '12 at 02:40
  • i dont understand what you mean, the fullmagazine object is being generated with the loadData function each time i start the app.. like all the other elements from SaveState class – NullPointerException Jun 05 '12 at 09:11
  • I dont have your full code. All I can do is assume some regular issues that may come due to serialization / de-serialization between versions of your application. The other 2 objects are serializable, but without knowing the composition of your FullMagazine implementation I cannot realize if its the root cause of your issue. So I am asking you to eliminate that object to isolate the issue to a android framework (between versions serialization) issue OR it may be a FullMagazine issue. There is nothing more I can say to help you out on this. – Siddharth Jun 05 '12 at 11:27
  • oh, the problem is not on FullMagazine, because the other objects are empty too. And they suposed to contain data.... – NullPointerException Jun 07 '12 at 07:47
  • You can try to mark fields `transient` to exclude this from deserialization and see if it will work with the rest of fields (so, if it has any "bad fields" which cause the issue). I have the same problem with my project and I actually just did the same, but with no success... I left one INT field which I know for sure is not zero in the stream. The rest I marked `transient`. Nothing actually changed, the int field is still deserialized as zero. – Mike Keskinov May 21 '18 at 17:28
  • I posted my solution to this problem here: https://stackoverflow.com/questions/19674993/objectinputstream-read-a-empty-object/50456011#50456011 – Mike Keskinov May 21 '18 at 20:14