Tom
Tom

Reputation: 557

cursor not returning column value

I'm getting a NullPointerException with a "prntln needs a message" error even though there is data in the cursor. What might I be missing here?

 databaseHelper = new DatabaseHelper(this);
 db = databaseHelper.getWritableDatabase();

 Cursor c = db.rawQuery("SELECT name, group FROM " +
             "groupWorksheet", null);

 if (c != null ) {

 if  (c.moveToFirst()) {
     Log.v(TAG, "has data");
       do {
            String myName = c.getString(c.getColumnIndex("name"));
            Log.v(TAG, myName);

       }while (c.moveToNext());
 }
 }else {

Log.v(TAG, "no data");
 }

Upvotes: 0

Views: 62

Answers (1)

ian.shaun.thomas
ian.shaun.thomas

Reputation: 3488

According to the documentation Cursor.getString() can return null.

You need to check if the result is null and if so, handle it properly. Just knowing the row exist does not verify that data exist in the row itself.

Upvotes: 1

Related Questions