Home > Blockchain >  Getting a CursorIndexOutOfBoundsException
Getting a CursorIndexOutOfBoundsException

Time:08-03

I have an application where the user enters data in a form, and can then view it in a table. Records can be selected in the table, causing that record to be loaded into the form where it can be edited or deleted. I am getting the following error message in Logcat,

android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0

in response to this cursor query,

edRowid.setText(c.getString(0)); 

Relevant Logcat output and code are below,

Logcat:

2022-08-02 09:30:08.630 5783-5783/net.pearl.GoldenBB E/AndroidRuntime: FATAL EXCEPTION: main
Process: net.pearl.GoldenBB, PID: 5783
android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
    at android.database.AbstractCursor.checkPosition(AbstractCursor.java:468)
    at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
    at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
    at net.pearl.GoldenBB.dataEdit.selectedTableRecord(dataEdit.java:223)
    at net.pearl.GoldenBB.dataEdit.onResume(dataEdit.java:173) 

Code:

  private void selectedTableRecord(){
    DBAdapter msdb= new DBAdapter(getActivity().getApplicationContext(),MainActivity.szDbName, null);
    db=msdb.getWritableDatabase();
    if(MainActivity.szSelectedRow == null){
       resetForm();
    }else{
        Cursor c = db.rawQuery("SELECT * FROM surveyDB WHERE _id =" MainActivity.szSelectedRow,null);
        c.moveToFirst();
EditText edRowid = (EditText) getView().findViewById(R.id.edRowidxml);
        edRowid.setText(c.getString(0)); //THIS IS LINE 223, WHERE FATAL EXCEPTION OCCURS
    c.close();  
    }
    db.close();
}

This code actually works fine when editing and updating records, however deleting a record results in the described error. Thanks in advance for useful insight and perspective.

UPDATE: Curiously, changing the column number from zero to one,

edRowid.setText(c.getString(0)); 

to this,

edRowid.setText(c.getString(1)); 

Causes the cursor query to run. Note that (0) is rowid whereas (1) is a data field.

UPDATE #2 Application also runs fine if I change the cursor query from this,

    edRowid.setText(c.getString(0)); 

to this,

    edRowid.setText("0"); 

What is odd is that there are several other columns that do cursor queries (c.getString("n")) and populate correctly. Column zero is rowid.

CodePudding user response:

... with a size of 0

means that the query did not return any rows.

This is why you should not just call moveToFirst(), which returns true if there is a first row or false if there is none, but check its value:

private void selectedTableRecord(){
    DBAdapter msdb = new DBAdapter(getActivity().getApplicationContext(), MainActivity.szDbName, null);
    db = msdb.getWritableDatabase();
    if (MainActivity.szSelectedRow == null) {
       resetForm();
    } else {
        Cursor c = db.rawQuery("SELECT * FROM surveyDB WHERE _id = ?", new String[] {MainActivity.szSelectedRow});
        if (c.moveToFirst()) {
            EditText edRowid = (EditText) getView().findViewById(R.id.edRowidxml);
            edRowid.setText(c.getString(0)); 
        } else {
            // do something
        }
        c.close();  
    }
    db.close();
}

I also used a ? placeholder for the parameter MainActivity.szSelectedRow and passed its value in the 2nd argument of rawQuery(), because this the recommended and safe way to pass parameters.

  • Related