J2ME InvalidRecordIDException Nokia 2700
Question
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);
}
Solution
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);
// ...
}
OTHER TIPS
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.