just a learner
just a learner

Reputation: 57

Exception while reading data through cursor from sqlite database in android

My data is successfully added into database.i have checked it through debugging my code. but when i read this data using cursor it throws following exception: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it. here is my code for database helper function:

                                `///get schools
                    public ArrayList<SchoolModel> getSchools() {

                        school.clear();

                        SQLiteDatabase db = this.getReadableDatabase();
                        Cursor cursor = db.rawQuery("select * from School", null);
                        if ((cursor.getCount() >0)){
                            if (cursor.moveToFirst()) {
                                do {
                                    SchoolModel item = new SchoolModel();
                                    item.SchoolID = cursor.getInt(cursor
                                            .getColumnIndex("SchoolID "));
                                    item.Address= cursor.getString(cursor
                                            .getColumnIndex("Address"));
                                    item.Cell= cursor.getString(cursor
                                            .getColumnIndex("Cell"));
                                    item.ContactPerson= cursor.getString(cursor
                                            .getColumnIndex("ContactPerson"));
                                    item.Description= cursor.getString(cursor
                                            .getColumnIndex("Description"));
                                    item.Phone= cursor.getString(cursor
                                            .getColumnIndex("Phone"));
                                    item.AreaID = cursor.getInt(cursor
                                            .getColumnIndex("AreaID"));


                                    school.add(item);

                                } while (cursor.moveToNext());
                            }
                        }
                        cursor.close();
                        db.close();
                        return school;
                    }

Can anybody tell me what am i doing wrong?

Upvotes: 0

Views: 363

Answers (1)

Blackbelt
Blackbelt

Reputation: 157437

 item.SchoolID = cursor.getInt(cursor  .getColumnIndex("SchoolID "));

probably removing the withe space after the capital D could help

Upvotes: 1

Related Questions