user717572
user717572

Reputation: 3652

SQLite cursor.getString() NullPointerException

I'm getting an NullPointerException when i'm trying to get data from my SQLiteDatabase in Android. I'm creating a Cursor from a raw query, and call cur.getString(index), however this results in a NullPointerException:

SQLiteDatabase db = dbHelper.getReadableDatabase();
    Cursor cur = db.rawQuery("SELECT Campaign, sum(Score) AS Score, Player FROM Levels GROUP BY Campaign", null);

    final int length = cur.getCount();
    names = new CampaignListItem[length];

    if (cur.moveToFirst()) {
        int row = 0;
        do {
            names[row].name = cur.getString(0); <--NPE
            names[row].score = cur.getInt(1);
            names[row].player = cur.getString(2);
            row++;
        } while (cur.moveToLast());
    }
    cur.close();
    db.close();  

I logged cur, and names, however these don't seem to be Null. Am I doing something wrong?

Upvotes: 0

Views: 1953

Answers (1)

ohra
ohra

Reputation: 506

You don't have any CampaignListItems in your array, since you only build the array but don't add any objects there. Therefore names[row] is null.

Upvotes: 2

Related Questions