views:

1374

answers:

7

I'm building a mobile app with J2ME, and I've found that the data I write into a RecordStore can be accessed while the program is still running but it is lost after quitting and restarting it. No exception is thrown, the data is simply lost.

UPDATE: Thanks everyone for your suggestions. I'm using NetBeans on Windows 7. I'm not sure if it is using the WTK version I have previously installed or another one it has installed somewhere else. I've checked my WTK folder for the files Pavel wrote about, but couldn't find them. Now I'm testing the features requiring persistence on my phone and everything else in the emulator, but it would of course be much better to be able to test everything in the emulator.

private RecordStore recordStore = null;

public MyMIDlet() {
    readStuff(); // output: nothing found in recordStore :(
    saveStuff();
    readStuff(); // output: stuff
}

private void readStuff() {
    try {
        recordStore = RecordStore.openRecordStore(REC_STORE, true);
        int n = recordStore.getNumRecords();
        String stuff;

        if (n == 0) {
            stuff = "nothing found in recordStore :(";
        }
        else {
            stuff = new String(recordStore.getRecord(1));
        }

        System.out.println(stuff);
    }
    catch (Exception e) {
        System.out.println("Exception occured in readStuff: " + e.getMessage());
    }
    finally {
        if (recordStore != null) {
            try {
                recordStore.closeRecordStore();
            }
            catch (Exception e) {
                // ignore
            }
        }
    }
}

private void saveStuff() {
    try {
        recordStore = RecordStore.openRecordStore(REC_STORE, true);
        int n = recordStore.getNumRecords();
        byte[] stuff = "stuff".getBytes();
        recordStore.addRecord(stuff, 0, stuff.length);
    } catch (Exception e) {
        System.out.println("Exception occured in saveStuff: " + e.getMessage());
    } finally {
        if (recordStore != null) {
            try {
                recordStore.closeRecordStore();
            } catch (Exception e) {
                // ignore
            }
        }
    }
}
A: 

I could finally get it to work on a real handset. It seems that, as Martin Clayton suggested, the emulator reset erased the data. I'm still looking for a way to enable persistence in the emulator though.

Botond Balázs
@Botond - check drubin and Pavel's answers - if you still have no luck with the emulator, maybe edit your question to add details of your OS and development setup - perhaps some more suggestion will be forthcoming.
martin clayton
+1  A: 

If you are using windows Vista there can and almost are permission issues. I am not sure how to resolve this but you might want to check that the user that is running the emulator has access to write to the emulator store.

In appdb/$phone/*.db

drubin
I'm using NetBeans, and it has installed the Java ME SDK in my home folder, C:\Users\Botond\javame-sdk, so I have write access to the folder, but it still doesn't work. Thanks anyway.
Botond Balázs
+2  A: 

If you use Sun WTK, it creates a file named "in.use" in its "appdb" folder:

C:\WTK25\appdb\DefaultColorPhone\in.use

If you close your emulator in unusual way (kill a process, for example), it would not delete it, and next time you run emulator, it would create temporary folder for storing data:

C:\WTK25\appdb\temp.DefaultColorPhone1

when starting this way, it should print in console: "Running with storage root temp.DefaultColorPhone1".

I fix it, including into my ".bat" file a line for deleting "in.use" file each time, emulator runs. But you should be careful when running several emulators at once.

Pavel Alexeev
Thank you. I've installed Sun WTK first, then NetBeans. I'm not really sure if NetBeans uses the version I installed or something else it has installed itself. There is no in.use file in my DefaultColorPhone folder and also no temp.DefaultColorPhone1 in appdb.
Botond Balázs
+1  A: 

I am using Netbeans with Jave ME 3.0 on Windows 7 x64 and still experience the same issue. It is extremely annoying, the folder listed in previous replies does not exist in a Java ME 3.0 install.

RishiD
+1  A: 

I experienced the same problem myself, I did however discover that NetBeans, or whatever, deletes the deployed program files after execution. These files are located in the C:\Documents and Settings\MyUser\javame-sdk\3.0\work\0\appdb folder, might be different on Vista/Win7 and I guess the number in the path refers to the emulator you are currently using. Anyways, in this folder look for something that is named like your RecordStore. E.g. "00000002_PSC_onfig.db", which is my suite configuration recordstore named PSConfig. By copying this to e.g. "Copy of 00000002_PSC_onfig.db" it will not be deleted. After NetBeans have cleaned up, just copy it back to its original name. The next time you hit run in NetBeans your recordstore will be there. It's pain, but at least it gives you the possibility to use the emulator to debug your RMS handling.

Master Ice
A: 

I m having a problem in storage of records. everytime I run a loop to write records to the record store, i loose the last 50 records. But if i run this loop 5-6 times den the records corresponding to the last loop are 50 less. Why is this happening ? Is there a way to overcome this problem...?

devyani
+1  A: 

Hi guys! to fix the storage problem you need to check the option "Storage size" in the netbeans platform manager. Go in project properties; go in platform; manage emulators; select the sun java wireless toolkit; go in Tools and Extensions; open preferences; storage; storage size option... set a size.

this works for me