2

I wrote a object using ObjectOutputStream, and read it using ObjectInputStream, and tested it , I get the expected result. But when write the object in other machine, read it in my computer, the read object's members are empty. Could someone help me? thanks

   public class TrustNet implements Serializable{

     public double[][] trusts;
     public double avg = 0;

     public TrustNet(int size){
        trusts = new double[size][size];
     }

    public void writeToFile(String fileName){
        try(ObjectOutputStream writer = new ObjectOutputStream(new  FileOutputStream(fileName))){
        writer.writeObject(this);
        writer.flush();
    } catch (IOException ex) {
        ex.printStackTrace();
    }
    }

   public static TrustNet readFromFile(String fileName){
    try(ObjectInputStream writer = new ObjectInputStream(new FileInputStream(fileName))){
        return (TrustNet) writer.readObject();
    } catch (IOException ex) {
        ex.printStackTrace();
    } catch (ClassNotFoundException ex) {
        ex.printStackTrace();
    }
    return null;
   }

  }
cstur4
  • 966
  • 2
  • 8
  • 21
  • 3
    share the code you have written till now – Scientist Oct 30 '13 at 06:07
  • `the read object's members are empty`, what do you mean? It returns `null`? – Eng.Fouad Oct 30 '13 at 06:11
  • @cstur4 Don't post code in comments. You can see for yourself that it's illegible. Edit it into your post. Please also show the writing and reading code, and state how you transported the file. – user207421 Oct 30 '13 at 06:19
  • thanks :) the empty object means its field is default value not what I saved. I just run the writeToFile function in a server, and copy the file to my local machine, I run the readFromFile function to get the TurstNet, but the fields are default values. It just work fine when write and read are all in my local machine. – cstur4 Oct 30 '13 at 06:23
  • I have the same problem. What could be the reason returing the deserialized object with nothing but default values in all the fields? In my case it works with one Android app version, but problem appears when users update to the next version of the app. Stored in internal storage file can't be read normally. – Mike Keskinov May 21 '18 at 18:28

1 Answers1

0

It took me a day to figure out what's going on. I think I finally have the answer. May be it will help other people having similar problem.

In my case, the problem was due of using ProGuard.

  1. ProGuard minifyed the code, giving new shorts names to fields, like a, b, c, etc.
  2. When serializing, the names of fields get stored into the stream. When reading the stream, the names must match.
  3. When I minifyed code the last time (for the new Production version of the app), ProGuard gave different names to fields (in my case it was due of changed ProGuard settings, but may be due of other reasons too, not sure if ProGuard guarantee the same names every time).
  4. As result, when I deserialize the object in the new version, because of the names are not match, all the fields set to default values.
Mike Keskinov
  • 11,614
  • 6
  • 59
  • 87