0

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?

1 Answer 1

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

probably removing the withe space after the capital D could help

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.