2

I have an RMS having 512 records as retrieved from

rs.getNumRecords();

But when I try to read the records using getRecordId(i) or even if using

rs.enumerateRecords( null, null, false );

I get InvalidRecordIDException, for records from 1 to 180, and I am able to read the records from 181 index to onwords.

I am not deleting the records anywhere in my application.

There have been instances of exiting the application in between while processing (reading this RMS), and overwriting the application.

I am closing the RMS at the end of my try block while reading the records.

Are there any possible reasons, of getting RMS corrupted or the particular indexes getting corrupted??

Code for reading the RMS using getRecord is

 try {
        RecordStore rs = RecordStore.openRecordStore(getTableName(), true);
        ByteArrayInputStream inputStream = null;
        DataInputStream inputDataStream = null;

       int nor = rs.getNumRecords();

      for(int i=1;i<=nor;i++)
      {
         System.out.println("i : "+i);

        _recordByteArr =  rs.getRecord(i);

        inputStream = new ByteArrayInputStream(_recordByteArr);
        inputDataStream = new DataInputStream(inputStream);

        System.out.println("val at " + i + " ::" + new String(_recordByteArr));

        //used inputDataStream.readUTF() here
      }
      if (inputDataStream != null) {
            inputDataStream.close();
      }
      if (inputStream != null) {
            inputStream.close();
      }

      rs.closeRecordStore();

    } catch (Exception e) {
        System.out.println("exp in read fieldtxn :" + e);
    }
Sandip Armal Patil
  • 6,241
  • 21
  • 93
  • 160
devsobhan
  • 25
  • 1
  • 6

2 Answers2

2

According to documentation on enumerateRecords method "This is the most efficient way to traverse all of the records in a record store."

I always use rs.enumerateRecords(null /*filter*/, null /*comparator*/, false /*keepUpdated*/) because I believe it is safer.

It is very odd that you are getting an InvalidRecordIDException from a RecordEnumeration traversal. Be sure to use it like this

RecordEnumeration re = rs.enumerateRecords( null, null, false );
while (re.hasNextElement()) {
    int i = re.nextRecordId();
    System.out.println("i : "+i);
    _recordByteArr =  rs.getRecord(i);
    // ...
}
Telmo Pimentel Mota
  • 4,033
  • 16
  • 22
  • 1
    Thanks. Using this code I am not getting InvalidRecordIdException, but still I am not able to retrieve records from 1 to 180. The Logs(system.out.println) print i from 512 to 181 and for 1 to 180 i gets printed as 0 i the above loop. Since the loop is iterating 512 times and id is getting printed from 512 to 181, and 0 for remaining records, is there some issue with records at 1 to 180?? – devsobhan Aug 07 '12 at 12:54
  • 1
    As you said "There have been instances of exiting the application in between while processing (reading this RMS), and overwriting the application." I believe your recordStore files might be corrupted. – Telmo Pimentel Mota Aug 07 '12 at 13:08
  • @TelmoPimentelMota Please , could you help me with this question? http://stackoverflow.com/questions/24342116/j2me-out-of-memory-exception-does-it-has-to-do-anything-with-the-maximum-heap – eddy Jun 21 '14 at 21:36
1

When you store records in RMS, it automatically creates a new ID for each the record getting store. This ID remains unique for each record. Suppose you have added 10 Records. Now if you delete 5th Records and add a new Record to RMS then this newly added record will get ID = 11.

While you are fetching the records using getRecords(int recNumber) method, when the recNumber value will be 5, it will return the Exception as InvalidRecordIDException as the 5th record is deleted.

I have created an RMS Library class. There is a method called getRecords() which is going to return the String Array as the records. You can use that String array to fetch the record you are looking for. It will be easy for you.

Community
  • 1
  • 1
Lucifer
  • 29,392
  • 25
  • 90
  • 143
  • Thanks for your reply. But I am not deleting any record through my application. Records are added, and I am simply reading those records. – devsobhan Aug 07 '12 at 09:52
  • Yes, that i read in your question. I just pointed that matter. – Lucifer Aug 07 '12 at 09:54