TheLettuceMaster
TheLettuceMaster

Reputation: 15734

SQLite Cursor Not Looping Correctly - Android

Here is method:

 public ArrayList<Integer> getDays() {
    Cursor c = database.rawQuery("SELECT * FROM debt;", null);

    nameList = new ArrayList<String>();
    dayList = new ArrayList<Integer>();

    String indName[] = new String[c.getCount()];
    String indDay[] = new String[c.getCount()];

    int d = 0, j = 0, u = 0;

    for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
        indName[j++] = c.getString(c.getColumnIndex("debt_name")); 
        indDay[d++] = c.getString(c.getColumnIndex("pay_day")); // line 384
    }   

    for (String name : indName) {
        if ((indDay[u] != "") || (indDay[u] != null)) {
            dayList.add(Integer.valueOf(indDay[u++]));

        }
    }
    c.close();

    return dayList;
}

I am getting an error at Line 384 that says: "Couldn't read row 0, col -1. Make sure cursor is initialized."

Line 384 is marked above. Can you find anything that I am missing here?

Upvotes: 0

Views: 130

Answers (2)

otskiexy
otskiexy

Reputation: 1

put c.moveToNext();

for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
        indName[j++] = c.getString(c.getColumnIndex("debt_name")); 
        indDay[d++] = c.getString(c.getColumnIndex("pay_day")); // line 384

    enter code here// c.moveToNext();
    }   

Upvotes: 0

remove ; from end of your Query : SELECT * FROM debt

call this line :

//optional
//"this"  is your DBHELPER class
SQLiteDatabase database = this.getWritableDatabase();

replace (marked for block) by this

if(c.moveToFirst()){
    while(c.moveToNext()) {
            indName[j++] = c.getString(c.getColumnIndex("debt_name"));
            indDay[d++] = c.getString(c.getColumnIndex("pay_day"));

        }   ( Line 386!!! )

}

Upvotes: 2

Related Questions