Rana Talha Tariq Rana Talha Tariq - 3 months ago 33
Android Question

ERROR: Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is correctly initialized before accessing data

I have created a very simple database android app. It takes an input and display result. Add button is used to add the input and delete button for deleting the input stored in SQLite database. My cursor in Android SQLite is pointed correctly but still I am facing the error: Caused by: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it. Following is the cursor part:
Cursor c = db.rawQuery(query, null);

c.moveToFirst();

//Traversing through DB
while (!c.isAfterLast()){
if(c.getString(c.getColumnIndex("studentname")) !=null)
{
dbString += c.getString(c.getColumnIndex("studentname"));
dbString +="\n"; }

c.moveToNext(); }
db.close();

return dbString;


I have tried changing cursor positions but still same error appearing. I have researched on other sites and same is the concept but still there is an issue.

Answer

Apparently, there is no studentname column in your result set, if c.getColumnIndex("studentname") is returning -1.

Comments