Slay29 Slay29 -4 years ago 105
Java Question

Cursor index out of bounds exception that is passing null check

I checked a bunch of questions here and I cannot find correct answer. I have problem with retrieving data from my database. I get

android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 0
exception. My code for creating database:

String KREIRAJ_TABELU = "CREATE TABLE " + IME_TABELE + "("
+ ID + " INTEGER PRIMARY KEY," + PITANJE + " TEXT,"
+ PRVI_NETACAN + " TEXT," + DRUGI_NETACAN + " TEXT," + TRECI_NETACAN + " TEXT," + TACAN_ODGOVOR + " TEXT" + ")";
db.execSQL(KREIRAJ_TABELU);
String dodajPitanjaIOdgovore = "INSERT INTO pitanja (ID,PITANJE,PRVI_NETACAN,DRUGI_NETACAN,TRECI_NETACAN,TACAN_ODGOVOR)\n" +
"VALUES (0,\"Ovo je prvo pitanje:\",\"Netacan odgovor\",\"Netacan odgovor\",\"Netacan odgovor\",\"Tacan odgovor\");\n" +
"\n" + ...
db.execSQL(dodajPitanjaIOdgovore);


And for retriving data I use this piece of code:

SQLiteDatabase db = this.getReadableDatabase();

Cursor cursor = db.query(IME_TABELE, new String[]{ID,
PITANJE, PRVI_NETACAN, DRUGI_NETACAN, TRECI_NETACAN, TACAN_ODGOVOR}, ID + "=?",
new String[]{String.valueOf(id)}, null, null, null, null);
if(cursor.getCount() > 0)
{
cursor.moveToFirst();
}
podaciOPitanjima podaci = new podaciOPitanjima(Integer.parseInt(cursor.getString(0)), cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getString(4), cursor.getString(5));
return podaci;


Edit: I also tried this:

if (cursor.moveToFirst()) {
podaciOPitanjima podaci = new podaciOPitanjima(Integer.parseInt(cursor.getString(0)), cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getString(4), cursor.getString(5));
}
else{
podaciOPitanjima podaci;
}
podaciOPitanjima podaci = new podaciOPitanjima(Integer.parseInt(cursor.getString(0)), cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getString(4), cursor.getString(5));
return podaci;

Answer Source

Change

if (cursor.getCount() > 0) {
    cursor.moveToFirst();
}
//do operations here

To

if (cursor.moveToFirst()) {
    //do operations here
}

Explanation - the problem is this line:

podaci = new poda...ima(Integer.parseInt(cursor.getString(0)), ...);

When you use cursor.getString the cursor is not actually at a row (because there is no row), so you cannot get a cell/column which is what this command is asking for.

The full solution:

SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(IME_TABELE, new String[]{ID, PITANJE, PRVI_NETACAN, DRUGI_NETACAN, TRECI_NETACAN, TACAN_ODGOVOR}, ID + "=?", new String[]{String.valueOf(id)}, null, null, null, null);
podaciOPitanjima podaci = null;
if (cursor.moveToFirst()) {
    //Note: using getColumnIndex avoids errors if the columns are ever returned in a changed order
    podaci = new podaciOPitanjima(
         cursor.getInteger( cursor.getColumnIndex(ID) ), 
         cursor.getString( cursor.getColumnIndex(PITANJE) ), 
         cursor.getString( cursor.getColumnIndex(PRVI_NETACAN) ), 
         cursor.getString( cursor.getColumnIndex(DRUGI_NETACAN) ), 
         cursor.getString( cursor.getColumnIndex(TRECI_NETACAN) ), 
         cursor.getString( cursor.getColumnIndex(TACAN_ODGOVOR) ));
}
return podaci;

Now remember wherever you are calling this method, that you might get null back and that you need to check for that.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download