You Are Here:

Community: Developer Discussion Boards

#1 Old getNumRecords/ null pointer exception - 2005-07-21, 18:04

Join Date: Jun 2005
Posts: 19
cjmarsha
Offline
Registered User
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.
Reply With Quote

#2 Old 2005-07-23, 14:40

Join Date: Jun 2003
Posts: 4,325
Location: Cheshire, UK
grahamhughes's Avatar
grahamhughes
Offline
Forum Nokia Champion
I'd guess if this code is throwing a NullPointerException, then "id" must be null. Perhaps the record store was never opened?

Graham.
Reply With Quote

#3 Old 2005-07-27, 23:35

Join Date: Jun 2005
Posts: 19
cjmarsha
Offline
Registered User
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();
}
Reply With Quote

#4 Old 2005-07-30, 14:09

Join Date: Jun 2003
Posts: 4,325
Location: Cheshire, UK
grahamhughes's Avatar
grahamhughes
Offline
Forum Nokia Champion
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();
}
What sort of results do you get from this?

Graham.
Reply With Quote
Reply « Previous Thread | Next Thread »
Display Modes
Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules

You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Forum Jump

Rate This

 
Bookmark this page: DeliciousDiggFacebookGoogleYahooStumbleUponRedditDiigoTechnocratiTwitter  Share this page Share this page Print this Page Print this page Invite a friend Invite a friend
京ICP备05048969号    Email Newsletters Press Terms & Conditions Privacy Policy Sitemap Contact Us © 2009 Nokia