| Reply | « Previous Thread | Next Thread » |
|
I'm trying to use getNumRecords() to find out if there are any records in a recordset. However when I use the following code a null pointer exception is generated.
try{ num = id.getNumRecords(); } catch (RecordStoreNotOpenException e) { //System.out.println("Record store exception"); } id is the recordstore variable, any suggestions on what might be wrong and why I'm getting a null pointer exception?? I really appreciate the help. |
|
I'd guess if this code is throwing a NullPointerException, then "id" must be null. Perhaps the record store was never opened?
Graham. |
| grahamhughes |
| View Public Profile |
| Find all posts by grahamhughes |
|
I no longer get a nullpointerexception, however for some reason the getNumRecords() is still not indicating that a record is stored. Even when i put the function immediately after the code that says addRecord, getNumRecords still returns zero, I really have no idea what is wrong with it, perhaps I am not adding the record properly. Here is the part of my code that is adding data to the recordset...
If I put the getNumRecords after id.addRecord(b, 0, b.length); the function should return 1 record right???? try{ id = RecordStore.openRecordStore("userID", true); } catch(RecordStoreException rse) { rse.printStackTrace(); } //saves data into a record store ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream dos = new DataOutputStream(baos); try{ //writes data to a record dos.writeUTF(record); } catch (IOException ioe) { System.out.println(ioe); ioe.printStackTrace(); } b = baos.toByteArray(); try { id.addRecord(b, 0, b.length); } catch (RecordStoreException rse) { System.out.println(rse); rse.printStackTrace(); } |
|
Are you running on an emulator or device? In either case, specifically which emulator or device?
You're catching exceptions in lots of places, and letting the code continue even if something has failed. I'd consider restructuring more like this: Code:
try {
RecordStore rs = RecordStore.openRecordStore("userID", true);
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
dos.writeUTF(record);
byte[] b = baos.toByteArray();
rs.addRecord(b, 0, b.length);
System.out.println ("gNR: " + rs.getNumRecords());
} finally {
rs.close();
}
} catch (RecordStoreException rse) {
rse.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
}
Graham. |
| grahamhughes |
| View Public Profile |
| Find all posts by grahamhughes |
| Reply | « Previous Thread | Next Thread » |
| Thread Tools | Search this Thread |
|---|---|