2012-02-11 83 views
0

當我試圖從Android中的SQLiteDatabase獲取數據時,出現NullPointerException。我創建從原始查詢上的遊標,並調用cur.getString(指數),然而這會導致一個NullPointerException:SQLite cursor.getString()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(); 

我登錄cur,並names,但這些似乎並沒有爲Null 。難道我做錯了什麼?

回答

2

您的數組中沒有任何CampaignListItems,因爲您只構建數組,但不在其中添加任何對象。因此,名稱[行]爲空。

+0

哦,上帝,我怎麼可能錯過了!我明天試試。 – user717572 2012-02-11 21:43:03

相關問題