简体   繁体   中英

Problems Using Cursor Read From android SQLite3 database

I can't for the life of me figure why this code is not working. It is following exactly the same format as other parts of my application save that I am passing two attributes to the helper method which should not be a problem and I am only returning one row which could be the problem but if it is I don't understand!

I have an activity that works until I call this command.

Log.d("Testing","Testing: " + cardID + " " + cardTypeID);
//LogCat displays the right values 11 1

Cursor c = mDbHelper.fetchCard(cardID,cardTypeID);  // <--COMMAND

The helper method looks like this

public Cursor fetchCard(String cardID,String cardTypeID) {  
    String sql = "SELECT teamName FROM tbl_team JOIN tbl_card ON tbl_team.teamID = tbl_card.teamID WHERE cardID=" + cardID;
    //This returns 1 row when run in a SQLite Database Browser

    Log.d("Testing","Testing :" + cardID + " " + cardTypeID );
    // Program has crashed before this Log
    // Caused by: java.lang.NullPointerException

    Cursor c;
    c =  mDb.rawQuery(sql, null);

    return c;

I'm leaning towards this being because there is only one row which has been read and I need to do a c.moveFirst() or something. Would apriciate help in working this out. Also why don't I get any write out to LogCat. It seems to fail straight away???

PS. I have tried return mDb.rawQuery(sql, null); Which I believe does the same as above.

Many Thanks Mark

Your mDbHelper is null. Initialize it. And learn to use LogCat instead of guessing with the logs the row that crashes the app, it really helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM