itay azaria
itay azaria

Reputation: 53

Cursor Index Out Of Bounds Exception (android SQLite)

I want to display data from column according to another column, so I used this method in HelperDB to get the Cursor:

public Cursor getData(String EMAIL) {
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor res =  db.rawQuery( "SELECT * FROM table_users WHERE email LIKE '" + EMAIL +"';" , null );
    return res;
}

And to display(hlp is the HlperDB):

Cursor c = hlp.getData(Email);
tv3 = (TextView) findViewById(R.id.tv3);
tv3.setText(c.getString(c.getColumnIndex(HelperDB.NOTES)));

But it gave this error: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1

Can you help me please?

Upvotes: 1

Views: 687

Answers (2)

jradich1234
jradich1234

Reputation: 1425

My guess is that your Cursor has no data (is NULL). The API documentations for Cursor.getColumneIndex (link) indicates that it will return -1 if the column does not exist. Then when you try and execute getString() you're passing a -1 and there's your error.

Upvotes: 0

Ferdous Ahamed
Ferdous Ahamed

Reputation: 21766

Try this:

Cursor c = hlp.getData(Email);
if(c != null)
    c.moveToFirst();

tv3 = (TextView) findViewById(R.id.tv3);
tv3.setText(c.getString(c.getColumnIndex(HelperDB.NOTES)));

Upvotes: 1

Related Questions