I have a database with the following method:
public void deleteNote(long rowId) {
mDb.delete(DATABASE_TABLE, KEY_REALROWID + "=" + rowId, null);
int x = (int) rowId;
int y = testCount();
while(x<y)
{x++;
Cursor note= fetchNote(x);
ContentValues argsan = new ContentValues();
argsan.put(KEY_REALROWID, x-1);
long z = Integer.valueOf(note.getString
(note.getColumnIndexOrThrow(NotesDbAdapter.K EY_ROWID)));
mDb.update(DATABASE_TABLE, argsan, KEY_ROWID + "=" + z, null);
}}
The insert method is this:
public void createNote(double value, String isroot, String ispower,
String ismultiply, String isdivisor, String add,
String issubtract, double roototpowerval, String
paranthaseesend, String paranthaseesstart) {
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_VALUE, value);
initialValues.put(KEY_ISROOT, isroot);
initialValues.put(KEY_ISPOWER, ispower);
initialValues.put(KEY_ISMULTIPLIER, ismultiply);
initialValues.put(KEY_ISDIVISOR, isdivisor);
initialValues.put(KEY_ISADD, add);
initialValues.put(KEY_ISSUBTRACT, issubtract);
initialValues.put(KEY_POWERORROOTNUMBER, roototpowerval);
initialValues.put(KEY_REALROWID, testCount()+1);
initialValues.put(KEY_ISPE, paranthaseesend);
initialValues.put(KEY_ISPS, paranthaseesstart);
mDb.insert(DATABASE_TABLE, null, initialValues);
}
The testCount() method is this:
public int testCount() {
Cursor c = mDb.rawQuery("select count(*) from notes", null);
int tst = 0;
if (c.moveToNext()) {
tst = c.getInt(c.getColumnIndex("count(*)"));
}
return tst;
}
The fetch method is this:
public Cursor fetchNote(long rowId) throws SQLException {
Cursor mCursor = mDb.query(true, DATABASE_TABLE, new String[]
{KEY_ISPS,KEY_ISPE,KEY_VALUE,
KEY_ISROOT,KEY_REALROWID,KEY_ISPOWER,KEY_POWERORROOTNUMBER,KEY_ISDIVISOR,
KEY_ISMULTIPLIER, KEY_ISADD,KEY_ISSUBTRACT},
KEY_REALROWID + "=" + rowId, null, null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
When I run this I get an error:
mDbHelper.createNote(d, "false", "false", "false", "false",
"false", "false", 0, "false", "false");
mDbHelper.createNote(0, "false", "false", "false","false",
"false", "true", 0, "false", "false");
mDbHelper.createNote(d, "false", "false", "false","false",
"false", "false", 0, "false", "false");
mDbHelper.createNote(d, "false", "false", "false","false",
"false", "false", 0, "false", "false");
mDbHelper.deleteNote(3);
Cursor c = mDbHelper.fetchNote(3);
Here is my LogCat:
FATAL EXCEPTION: main
android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
at android.database.AbstractCursor.checkPosition(AbstractCursor.java:580)
at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:214)
at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:41)
at com.real.AlgebraActivity$1.onClick(AlgebraActivity.java:301)
at android.view.View.performClick(View.java:2485)
at android.view.View$PerformClick.run(View.java:9080)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:3683)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)
As always thanks for any help.
Ok i think i found the root of your
Exception.In the activity
onClick()method you create(and insert in the DB) 4 notes, delete one(note 3) and then you fetch the note 3(this should be note 4 that has become note 3). What you didn’t post post from that method is other lines after the lineCursor c = mDbHelper.fetchNote(3);where most likely you try to use that cursorc.The problem is that your
deleteNote()method works as you designed it:– deletes the note with the id (3 in you example)
–
x=3andybecomes 3(you have deleted one row from the 4 that you inserted) so when you get to thewhileloop (x < y) the condition is false so you never enter in to the loop to update the note 4 to become note 3.Now your database has a structure like this:
Then you try to fetch the note 3 that doesn’t exist and you end up with an empty cursor
con which you try to work and throws theCursorIndexOutOfBoundsException.To resolve the issue try to update your logic so you don’t end up with empty spaces in the
KEY_REALROWIDcolumn. (I haven’t tested but try to putwhile(x <= y)in thedeleteNote()method).Well this is what i think it’ happening.