2

I'm trying to use lwuit.io.Storage, it works fine while application is running (I can do crud) but it's data clears after closing the application. I've considered:

  • registering my class with "Util.register("Car", Car.class);" in "initVars()" method;
  • implementing "Externalizable" interface in "Car" class. // "getObjectId()" method returns "Car"
  • calling "Storage.init("myProjectName");" in MainMIDlet class
  • checking to make sure Storage is initialized properly with "Storage.isInitialized()" in MainMIDlet class
  • I'm storing my "car" objects with a Vector, using:

    Car c = new Car("ford", 1918);
    
    Vector v = (Vector) Storage.getInstance().readObject("cars");
    if (v == null) {
        v = new Vector();
    }
    v.addElement(c);
    Storage.getInstance().writeObject("cars", v);
    
    Vector test = (Vector) Storage.getInstance().readObject("cars");
    if (test.lastElement() instanceof Car){
       Car c1 = (Car) test.lastElement();
       System.err.println(c1);
    }
    
  • avoiding call "Storage.getInstance().clearStorage();" anywhere in the code!

  • "Storage.getInstance().listEntries();" returns one element array containing "cars" String (even after restarting application).

this is my simple "car" class.

any idea what I'm missing / doing wrong here?

thanks in advance

Ali Ghanavatian
  • 506
  • 1
  • 6
  • 14
  • I tried to test with a "String" instead of "Vector", it worked fine. the string preserved in the Storage after closing the app. – Ali Ghanavatian Mar 05 '12 at 17:32

1 Answers1

2

I got it, that was a stupid mistake,

The problem was in implementing Externalizable inteface, I was calling "Util.writeUTF(name);" in externalize() method, and trying to read it with "stream.readUTF()" in internalize method.

simply replaced "Util.writeUTF(name)" with "stream.writeUTF(name)".

and it worked out.

Ali Ghanavatian
  • 506
  • 1
  • 6
  • 14