【问题标题】:SQLite cursor.getString() NullPointerExceptionSQLite cursor.getString() NullPointerException
【发布时间】:2012-02-11 21:34:25
【问题描述】:

当我尝试从 Android 中的 SQLiteDatabase 获取数据时,我收到了 NullPointerException。我正在从原始查询创建游标,并调用 cur.getString(index),但这会导致 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();  

我登录了 curnames,但这些似乎不是 Null。我做错了吗?

【问题讨论】:

    标签: android sqlite cursor nullpointerexception


    【解决方案1】:

    您的数组中没有任何 CampaignListItems,因为您只构建数组但没有在其中添加任何对象。因此 names[row] 为空。

    【讨论】:

    • 天哪,我怎么会错过!我明天试试。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-08
    • 2016-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多