Mark Quinn
Mark Quinn

Reputation: 33

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

Upvotes: 0

Views: 317

Answers (1)

ernazm
ernazm

Reputation: 9258

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.

Upvotes: 1

Related Questions