CursorIndexOutOfBoundsException:请求索引0,大小为0?

时间:2016-08-25 07:30:23

标签: java android sqlite

我想从我的sqlite数据库中获取一行。当我运行这行代码时: -

  RReckoner_info rReckoner_info = dbHandler.get_subcategory(1);
        Log.d("INFO : ", rReckoner_info.getCategory_name());

我收到以下错误: -

  

android.database.CursorIndexOutOfBoundsException:请求索引0,   大小为0

  public RReckoner_info get_subcategory(int id)
    {
        SQLiteDatabase db = this.getReadableDatabase();

        Cursor cursor = db.query(TABLE_SUBCATEGORY_LIST, new String[]{KEY_NAME,KEY_DESC, KEY_CATEGORY_ID, KEY_CONTENT1,KEY_CONTENTTYPE1,KEY_CONTENT2,KEY_CONTENTTYPE2,KEY_CONTENT3,KEY_CONTENTTYPE3,
                KEY_CONTENT4,KEY_CONTENTTYPE4,KEY_CONTENT5,KEY_CONTENTTYPE5,KEY_CONTENT6,KEY_CONTENTTYPE6,KEY_ORDERID,KEY_STATUS ,KEY_UPDATED},KEY_NAME +"=?",
                new String[]{String.valueOf(id)},null,null, null,null);
        if (cursor !=null)
        {
            cursor.moveToFirst();
        }

        RReckoner_info rReckoner_info = new RReckoner_info(cursor.getString(0),cursor.getString(1),Integer.parseInt(cursor.getString(2)),
                cursor.getString(3),cursor.getString(4),cursor.getString(5),cursor.getString(6)
                ,cursor.getString(7),cursor.getString(8),cursor.getString(9),cursor.getString(10),
                cursor.getString(11),cursor.getString(12),cursor.getString(13),cursor.getString(14),
                Integer.parseInt(cursor.getString(15)),cursor.getString(16),cursor.getString(17));

        return rReckoner_info;

    }

任何人都可以帮我解决这个问题。感谢任何帮助或建议。谢谢。

3 个答案:

答案 0 :(得分:1)

更改以下代码

来自:

if (cursor !=null)
    {
        cursor.moveToFirst();
    }
RReckoner_info rReckoner_info = new RReckoner_info(cursor.getString(0),cursor.getString(1),Integer.parseInt(cursor.getString(2)),
            cursor.getString(3),cursor.getString(4),cursor.getString(5),cursor.getString(6)
            ,cursor.getString(7),cursor.getString(8),cursor.getString(9),cursor.getString(10),
            cursor.getString(11),cursor.getString(12),cursor.getString(13),cursor.getString(14),
            Integer.parseInt(cursor.getString(15)),cursor.getString(16),cursor.getString(17));

return rReckoner_info;

收件人:

if (cursor !=null && cursor.getCount() > 0)
    {
        cursor.moveToFirst();
        RReckoner_info rReckoner_info = new RReckoner_info(cursor.getString(0),cursor.getString(1),Integer.parseInt(cursor.getString(2)),
            cursor.getString(3),cursor.getString(4),cursor.getString(5),cursor.getString(6)
            ,cursor.getString(7),cursor.getString(8),cursor.getString(9),cursor.getString(10),
            cursor.getString(11),cursor.getString(12),cursor.getString(13),cursor.getString(14),
            Integer.parseInt(cursor.getString(15)),cursor.getString(16),cursor.getString(17));

    return rReckoner_info;
    }
return null;

如果没有找到行,我将返回null,您可以根据您的要求更改最后一部分,但是您必须检查光标的count以避免CursorIndexOutOfBoundsException

答案 1 :(得分:0)

更改为

    if (cursor !=null)
    {
        cursor.moveToFirst();

       RReckoner_info rReckoner_info = new RReckoner_info(cursor.getString(0),cursor.getString(1),Integer.parseInt(cursor.getString(2)),
            cursor.getString(3),cursor.getString(4),cursor.getString(5),cursor.getString(6)
            ,cursor.getString(7),cursor.getString(8),cursor.getString(9),cursor.getString(10),
            cursor.getString(11),cursor.getString(12),cursor.getString(13),cursor.getString(14),
            Integer.parseInt(cursor.getString(15)),cursor.getString(16),cursor.getString(17));

        return rReckoner_info;
   }
   return null;

答案 2 :(得分:0)

您的查询没有提取任何记录,因为CursorIndexOutOfBoundsException表示:

  

指示光标超出范围的异常。

检查是否至少有记录:

 if (cursor !=null && cursor.getCount() > 0) {
     cursor.moveToFirst();


   // Code to extract data
}

或者您可以执行以下操作,因为如果没有记录,moveToFirst将返回false

  

如果光标为空,此方法将返回false。

if (cursor != null && cursor.moveToFirst()) {
    // Code to extract data
}