Could someone please help me to understand why the following block of code keeps throwing this error? It’s driving me crazy trying to debug this.
public int getContactsCountByGroupId(int id) {
Cursor c = db.rawQuery("SELECT COUNT(*) AS total FROM msg_group_lu WHERE group_id = ?", new String[] {String.valueOf(id)});
DatabaseUtils.dumpCursor(c);
int retval = c.getInt(c.getColumnIndex("total"));
return retval;
}
The dump seems to indicate this;
0 {
total=0
}
This is telling me that there is data in column 0, but yet everytime it tries to execute the line with this code;
int retval = new Integer(c.getInt(c.getColumnIndex("total")));
It gives me this error;
CursorIndexOutOfBoundsException: Index -1 requested, with size of 1
I have tried everything I can think of to try to fix this and am completely stumped. 🙁
I hope someone knows what causes this.
try writing
c.moveToNext();before executing the lineThe cursor position is now -1. after executing the moveToNext command it comes to 0th position.