Reputation: 954
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"
Upvotes: 4
Views: 15397
Reputation: 67286
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
}
Upvotes: 18
Reputation: 1721
You select the _id
column, yet you use mCursor.getColumnIndex("hw")
. There is no column hw
in your selection, only _id
. getColumnIndex()
returns 0
, you get corresponding error.
UPD: There is no need to use getCount()
. Let me share some code snippets. Firstly, let's assume that we need only the first value from the cursor:
Cursor cursor = //query here
if (cursor.moveToFirst()) {
// retrieve values from the cursor
}
If we need more than one value:
Cursor cursor = //query here
while (cursor.moveToNext()) {
// retrieve next set of values from the cursor
}
The initial position of the cursor is always -1
, that's why the above code will work and process all entries.
Upvotes: 3