I started out with RMS lately(well last hour only) and laid out methods for my class to use RMS for data persistency. The MIDlet works fine both in my S40 Nokia cell & the emulator(Sun Java) as long as the App is not closed.

Steps
1. I add record to my class
2. I update database
3. I erase the object(list)
4. I redeem the object(list) from the database

Everything works fine till this point. But as soon as I close the App, for some strange reason the persistent RMS is persistent no more and upon opening I get an Exception saying no record exists.

My Object's Class' methods:

.
.
.
private RecordStore dataStore;

public void openRecordStore(boolean canCreate) throws RecordStoreException
        {
            dataStore = RecordStore.openRecordStore("NX Record Master", canCreate);
        }
        
        public void closeRecordStore() throws RecordStoreException
        {            
            dataStore.closeRecordStore();            
        }

         public void deleteRecordStore() throws RecordStoreException
        {
            RecordStore.deleteRecordStore("NX Record Master");
        }


        private Credentials fromByteArray( byte[] data) throws IOException
        {
            ByteArrayInputStream bin = new ByteArrayInputStream(data);
            DataInputStream din = new DataInputStream( bin );
            Credentials currUser=new Credentials();

            currUser.setEmail(din.readUTF());
            currUser.setUserName(din.readUTF());
            currUser.setPassWord(din.readUTF());
            currUser.setComment(din.readUTF());

            din.close();
            return currUser;
        }

        private byte[] toByteArray(Credentials currUser) throws IOException
        {
            ByteArrayOutputStream bout = new ByteArrayOutputStream();
            DataOutputStream dout = new DataOutputStream( bout );

            dout.writeUTF( currUser.getEmail() );
            dout.writeUTF( currUser.getUserName() );
            dout.writeUTF( currUser.getPassWord() );
            dout.writeUTF( currUser.getComment() );

            dout.close();

            return bout.toByteArray();
        }

        public boolean storeDataRecord()
        {
            for(int i=0; i<userCount; i++)
            {
                try
                {
                    byte[] data = this.toByteArray(this.getCredentials()[i]);
                    dataStore.addRecord( data, 0, data.length );
                }
                catch( RecordStoreFullException e )
                {
                    return false;
                }
                catch( RecordStoreException e )
                {
                    // handle the RMS error here
                    e.printStackTrace();
                }
                catch( IOException e )
                {
                    // handle the IO error here
                    e.printStackTrace();                    
                }
            }
            return true;
        }

        public boolean retrieveDataRecord()
        {
            int maxRecords=0;
            userCount=0;

            try
            {
                maxRecords=dataStore.getNumRecords();
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
            for(int i=1; i<=maxRecords; i++)
            {
                try
                {
                    Credentials targetUser, thisUser;
                    thisUser = this.fromByteArray(dataStore.getRecord(i));
                    targetUser = this.getCredentials()[getCount()];

                    if(targetUser==null)
                        targetUser = new Credentials();

                    this.getCredentials()[this.getCount()].setEmail(thisUser.getEmail());
                    this.getCredentials()[this.getCount()].setUserName(thisUser.getUserName());
                    this.getCredentials()[this.getCount()].setPassWord(thisUser.getPassWord());
                    this.getCredentials()[this.getCount()].setComment(thisUser.getComment());

                    this.addCount();
                }
                catch( RecordStoreException e )
                {
                    // handle the RMS error here
                    e.printStackTrace();
                    //return false;
                }
                catch( IOException e )
                {
                    // handle the IO error here
                    e.printStackTrace();
                    //return false;
                }
            }
            return true;
        }

And the wrapping is provided by the functions:

public boolean saveUserData()
    {
        try
        {
            User.openRecordStore(true);
            if(User.storeDataRecord())
                infoBox("Records have been updated.", getMainMenu());
            else
                errorBox("Error saving. Out of memory.", getMainMenu());
            User.closeRecordStore();
        }
        catch(Exception e)
        {
            errorBox("Record Store cannot be opened.", getMainMenu());
            return false;
        }

        return true;
    }

    public boolean fetchUserData()
    {
        try
        {
            User.openRecordStore(false);
            if(User.retrieveDataRecord())
                infoBox("Records have been loaded.", getMainMenu());
            else
                errorBox("Error fetching records.", getMainMenu());
            User.closeRecordStore();
        }
        catch(Exception e)
        {
            errorBox("Record Store cannot be opened.", getMainMenu());
            return false;
        }

        return true;
    }

And as stated everything is fine till I infact close the application and upon reopening I get "Record Store cannot be opened."

According to the Sun Documentation Record Stores are Persistent but I can't figure out why I'm not getting the desired result.

Thanks,
Nisheeth Barthwal

Recommended Answers

All 5 Replies

Instead of printing custom messages use printStackTrace() and post it back here

Instead of printing custom messages use printStackTrace() and post it back here

Well when I close and restart the app (i.e. Exit from commands, do not close the emulator)
and I tried to read it back, I get the RecordStoreNotFoundException. It isn't able to find the record Store at all. Anything to do with emulator settings?

Please post stack trace and not only whatever left overs JVM throws at you

javax.microedition.rms.RecordStoreNotFoundException: cannot find record store file
        at com.sun.midp.rms.RecordStoreImpl.<init>(), bci=65
        at com.sun.midp.rms.RecordStoreImpl.openRecordStore(), bci=13
        at javax.microedition.rms.RecordStore.doOpen(), bci=118
        at javax.microedition.rms.RecordStore.openRecordStore(), bci=15
        at RecordMaster.Users.openRecordStore(Users.java:52)
        at RecordMaster.RecordMasterMIDlet.fetchUserData(RecordMasterMIDlet.java:354)
        at RecordMaster.RecordMasterMIDlet.mainMenuAction(RecordMasterMIDlet.java:929)
        at RecordMaster.RecordMasterMIDlet.commandAction(RecordMasterMIDlet.java:186)
        at javax.microedition.lcdui.ChoiceGroupLFImpl.uCallKeyPressed(), bci=271
        at javax.microedition.lcdui.FormLFImpl.uCallKeyPressed(), bci=87
        at javax.microedition.lcdui.DisplayableLFImpl.uCallKeyEvent(), bci=146
        at javax.microedition.lcdui.Display$DisplayEventConsumerImpl.handleKeyEvent(), bci=30
        at com.sun.midp.lcdui.DisplayEventListener.process(), bci=277
        at com.sun.midp.events.EventQueue.run(), bci=179
        at java.lang.Thread.run(Thread.java:619)

Thanks for your interest pester_budo. I however got the fault:

//Like totally my Bad! :(
//Since the function was wrapped I never got this exception thrown
targetUser = this.getCredentials()[getCount()];
if(targetUser==null)
        targetUser = new Credentials();
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.