Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cursor is crashing: Index 0 requested, with a size of 0

I don't know why my cursor is empty. I read other 4-5 questions which contain the same message but I couldn't fix my problem. What did I forget?

With this code I got the message: "Cursor is empty":

    public void onClick(View arg0) {
        String txt_edit_hw = edit_hw.getText().toString(); 
        ContentValues args = new ContentValues();
        args.put("hw", txt_edit_hw);

        String test = "SELECT _id FROM tbl_homework where hw='"+ txt_edit_hw +"';";

        Cursor mCursor = db.rawQuery(test, null); 

        startManagingCursor(mCursor);
        if (mCursor.moveToFirst()){

        String hw = mCursor.getString(mCursor.getColumnIndex("hw"));


        doMessage(hw);
        db.update("tbl_homework", args, "_id=34", null);
        fillData();
        } else {
            doMessage("Cursor is empty");
        }
    }

With this code I got the crach... android Index 0 requested, with a size of 0:

    public void onClick(View arg0) {
        String txt_edit_hw = edit_hw.getText().toString(); 
        ContentValues args = new ContentValues();
        args.put("hw", txt_edit_hw);

        String test = "SELECT _id FROM tbl_homework where hw='"+ txt_edit_hw +"';";

        Cursor mCursor = db.rawQuery(test, null); 

        startManagingCursor(mCursor);
        mCursor.moveToFirst();

        String hw = mCursor.getString(mCursor.getColumnIndex("hw"));

        doMessage(hw);
        db.update("tbl_homework", args, "_id=34", null);
        fillData();
    }

Same error this way: It crashes at the String (I commented it)

        public void onClick(View arg0) {
        String txt_edit_hw = edit_hw.getText().toString(); 
        txt_edit_hw.trim();
        ContentValues args = new ContentValues();
        args.put("hw", txt_edit_hw);

        String test = "SELECT _id FROM tbl_homework where hw='"+ txt_edit_hw +"';";

        Cursor mCursor = db.rawQuery(test, null); 

        startManagingCursor(mCursor);
        mCursor.moveToFirst();

        /**It crashes here**/String hwl = mCursor.getString(mCursor.getColumnIndex("_id"));

        doMessage(hwl);
        db.update("tbl_homework", args, "_id=34", null);
        fillData();
    }

02-06 12:13:49.789: I/Process(6112): Sending signal. PID: 6112 SIG: 9
02-06 12:13:55.582: D/dalvikvm(6140): GC_EXTERNAL_ALLOC freed 37K, 49% free 2785K/5379K, external 5211K/5218K, paused 23ms
02-06 12:13:58.785: D/AndroidRuntime(6140): Shutting down VM
02-06 12:13:58.785: W/dalvikvm(6140): threadid=1: thread exiting with uncaught exception (group=0x40091568)
02-06 12:13:58.796: E/AndroidRuntime(6140): FATAL EXCEPTION: main
02-06 12:13:58.796: E/AndroidRuntime(6140): android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
02-06 12:13:58.796: E/AndroidRuntime(6140):     at android.database.AbstractCursor.checkPosition(AbstractCursor.java:580)
02-06 12:13:58.796: E/AndroidRuntime(6140):     at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:214)
02-06 12:13:58.796: E/AndroidRuntime(6140):     at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:41)
02-06 12:13:58.796: E/AndroidRuntime(6140):     at test.marco.notenha.homework$3.onClick(homework.java:227)
02-06 12:13:58.796: E/AndroidRuntime(6140):     at android.view.View.performClick(View.java:2486)
02-06 12:13:58.796: E/AndroidRuntime(6140):     at android.view.View$PerformClick.run(View.java:9130)
02-06 12:13:58.796: E/AndroidRuntime(6140):     at android.os.Handler.handleCallback(Handler.java:587)
02-06 12:13:58.796: E/AndroidRuntime(6140):     at android.os.Handler.dispatchMessage(Handler.java:92)
02-06 12:13:58.796: E/AndroidRuntime(6140):     at android.os.Looper.loop(Looper.java:130)
02-06 12:13:58.796: E/AndroidRuntime(6140):     at android.app.ActivityThread.main(ActivityThread.java:3703)
02-06 12:13:58.796: E/AndroidRuntime(6140):     at java.lang.reflect.Method.invokeNative(Native Method)
02-06 12:13:58.796: E/AndroidRuntime(6140):     at java.lang.reflect.Method.invoke(Method.java:507)
02-06 12:13:58.796: E/AndroidRuntime(6140):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
02-06 12:13:58.796: E/AndroidRuntime(6140):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
02-06 12:13:58.796: E/AndroidRuntime(6140):     at dalvik.system.NativeStart.main(Native Method)

Table: Rows: _id, hwdate, hw Val : "34" , "05/02/12", "hy,xsdhagsah"

like image 942
Marco Seiz Avatar asked Dec 02 '22 00:12

Marco Seiz


1 Answers

Simply it says that your Cursor is not having any value and still you are trying to read from your Cursor. So, its always better to check the value of cursor.getCount();

Log.d("Count",String.valueOf(cursor.getCount())); 
if(cursor.getCount() > 0){
// get values from cursor here
}
like image 157
Lalit Poptani Avatar answered Dec 04 '22 13:12

Lalit Poptani