Java – CursorIndexOutOfBoundException of size 1 : Index 1 requested,

CursorIndexOutOfBoundException of size 1 : Index 1 requested,… here is a solution to the problem.

CursorIndexOutOfBoundException of size 1 : Index 1 requested,

Me again, there’s a problem with Android’s sqlite

I am currently getting “CursorIndexOutOfBoundsException: Request index 1 with size 1″
However, I got an exception of index -1 and inserted a cursor.moveToFirst(), then I have this index of 0 and then cursor.moveToNext();
What do I want to do with my code? I want information about the selected project (that’s why selectionArgs exists with sQueryid. What am I doing wrong?

    Cursor c = a.managedQuery(uri, 
            projection, //projection
            "_ID=?", //selection string
            new String[]{sQueryid}, //selection args array of strings
            DepotTableMetaData.DEFAULT_SORT_ORDER); sort order
    c.moveToFirst();
    c.moveToNext();
    int iqrcode = c.getColumnIndex(ContentProviderMetaData.DepotTableMetaData.ITEM_QRCODE);     
    int iname = c.getColumnIndex(ContentProviderMetaData.DepotTableMetaData.ITEM_NAME);
    int iamount = c.getColumnIndex(ContentProviderMetaData.DepotTableMetaData.ITEM_AMOUNT);
    int iunit = c.getColumnIndex(ContentProviderMetaData.DepotTableMetaData.ITEM_UNIT);
    int ippu = c.getColumnIndex(ContentProviderMetaData.DepotTableMetaData.ITEM_PPU);
    int itotal = c.getColumnIndex(ContentProviderMetaData.DepotTableMetaData.ITEM_TOTAL);
    int icomment = c.getColumnIndex(ContentProviderMetaData.DepotTableMetaData.ITEM_COMMENT);

Gather values
        String id = c.getString(Integer.parseInt(queryid.toString()));

String name = c.getString(iname);
        String amount = c.getString(iamount);
        String unit = c.getString(iunit);
        String ppu = c.getString(ippu);
        String total = c.getString(itotal);
        String comment = c.getString(icomment);
        String qrcode = c.getString(iqrcode);

String[] info = new String[]{id,name,amount,unit,ppu,total,comment,qrcode};

Solution

moveToFirst takes you to the first (and only) result. The next call to moveToNext takes you to a second result (index 1) that does not exist.

Try removing c.moveToNext();

Related Problems and Solutions