1

I have a simple SQlite class. Adding data to it seems to work without an issue. When I try to access the database's data I get:

java.lang.IllegalStateException: Couldn't read row 0, col 3 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.

I know that it is 0 based from that thread. So this is not the problem. The weird thing about it is, that I can reach the complete rows of the table and it runs into an error when it tries to return the data. If there is more data in the database I get more rows before the app crashes somewhere in the middle of the table.

While debugging I see the program running one time through the loop and and leaving it after that. The program does not seem to crash at db.close() but on return dbMaschineList.

The complete console output:

06-02 14:56:59.687 31524-31524/com.stack.cloud E/Database Problem: java.lang.IllegalStateException: Couldn't read row 0, col 3 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it. 06-02 14:57:00.438 31524-31524/com.stack.cloud E/Database Problem: java.lang.IllegalStateException: Couldn't read row 0, col 3 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it. 06-02 14:57:00.504 31524-31524/com.stack.cloud E/Database Problem: java.lang.IllegalStateException: Couldn't read row 0, col 3 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it. 06-02 14:57:00.991 31524-31524/com.stack.cloud E/Database Problem: java.lang.IllegalStateException: Couldn't read row 0, col 3 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it. 06-02 14:57:01.234 31524-31524/com.stack.cloud E/Database Problem: java.lang.IllegalStateException: Couldn't read row 0, col 3 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it. 06-02 15:02:39.616 4962-4962/com.stack.cloud E/Database Problem: java.lang.IllegalStateException: Couldn't read row 0, col 3 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it. 06-02 15:02:39.648 4962-4962/com.stack.cloud E/Database Problem: java.lang.IllegalStateException: Couldn't read row 0, col 3 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it. 06-02 15:02:39.690 4962-4962/com.stack.cloud E/Database Problem: java.lang.IllegalStateException: Couldn't read row 0, col 3 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it. 06-02 15:02:40.020 4962-4962/com.stack.cloud E/Database Problem: java.lang.IllegalStateException: Couldn't read row 0, col 3 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it. 06-02 15:02:40.066 4962-4962/com.stack.cloud E/Database Problem: java.lang.IllegalStateException: Couldn't read row 0, col 3 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it. 06-02 15:05:10.224 7565-7565com.stack.cloud E/Database Problem: java.lang.IllegalStateException: Couldn't read row 0, col 3 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it. 06-02 15:05:10.236 7565-7565/com.stack.cloud E/Database Problem: java.lang.IllegalStateException: Couldn't read row 0, col 3 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it. 06-02 15:05:10.249 7565-7565/com.stack.cloud E/Database Problem: java.lang.IllegalStateException: Couldn't read row 0, col 3 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it. 06-02 15:05:10.261 7565-7565/com.stack.cloud E/Database Problem: java.lang.IllegalStateException: Couldn't read row 0, col 3 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it. 06-02 15:05:10.272 7565-7565/com.stack.cloud E/Database Problem: java.lang.IllegalStateException: Couldn't read row 0, col 3 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.

    public class DBHandler extends SQLiteOpenHelper implements MaschineListener {
        private static final int DB_VERSION = 1;
        private static final String DB_NAME = "maschinedb.db";
        private static final String TABLE_NAME = "maschine";
        private static final String KEY_ID = "_id";
        private static final String KEY_NAME = "_name";
        private static final String KEY_DESCRIPTION = "_description";

        String CREATE_TABLE = "CREATE TABLE "+TABLE_NAME+" ("+ KEY_ID +" INTEGER PRIMARY KEY,"+KEY_NAME+" TEXT,"+KEY_DESCRIPTION+" TEXT)";
    String DROP_TABLE = "DROP TABLE IF EXISTS "+TABLE_NAME;

        public DBHandler(Context context) {
            super(context, DB_NAME, null, DB_VERSION);
        }


        @Override
        public void addMaschine(Maschine dbMaschine) {
            SQLiteDatabase db = this.getWritableDatabase();
            try{
                ContentValues values = new ContentValues();
                values.put(KEY_NAME, dbMaschine.getName());
                values.put(KEY_DESCRIPTION, dbMaschine.getDescription());
                db.insert(TABLE_NAME, null, values);
                db.close();
            }catch (Exception e){
                Log.e("Database Problem", e + "");
            }
        }

        @Override
        public ArrayList<Maschine> getAllMaschine() {
            SQLiteDatabase db = this.getReadableDatabase();
            ArrayList<Maschine> dbMaschineList = null;
            try{
                dbMaschineList = new ArrayList<Maschine>();
                   String QUERY = "SELECT " + KEY_ID +", " + KEY_NAME + ", " + KEY_DESCRIPTION + " FROM "+ TABLE_NAME;
            Cursor cursor = db.rawQuery(QUERY, null);
cursor.moveToFirst();

           if (cursor.moveToFirst()) {
            do {
                    Maschine dbMaschine = new Maschine();
                    dbMaschine.setId(cursor.getInt(0));
                    dbMaschine.setName(cursor.getString(1));
                    dbMaschine.setDescription(cursor.getString(2));
                    dbMaschineList.add(dbMaschine);
            } while (cursor.moveToNext());
        }
        cursor.close();

        db.close();
            }catch (Exception e){
                Log.e("Database Problem", e + "");
            }

            return dbMaschineList;
        }

        @Override
        public int getMaschineCount() {
            int num = 0;
            SQLiteDatabase db = this.getReadableDatabase();
            try{
                String QUERY = "SELECT * FROM "+ TABLE_NAME;
                Cursor cursor = db.rawQuery(QUERY, null);
                num = cursor.getCount();
                db.close();
                return num;
            }catch (Exception e){
                Log.e("error",e + "");
            }

            return 0;
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL(CREATE_TABLE);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            db.execSQL(DROP_TABLE);
            onCreate(db);
        }
    }

my database: enter image description here

When I run the app the first time, after deleting it from the storage before I get:

|| 1 || CNC || Metallverarbeitung

Community
  • 1
  • 1
jublikon
  • 3,427
  • 10
  • 44
  • 82
  • Can you show on which line are you getting the error? – Mauker Jun 02 '16 at 13:33
  • @Selvin I have changed the query to: String QUERY = "SELECT " + KEY_ID +", " + KEY_NAME + ", " + KEY_DESCRIPTION + " FROM "+ TABLE_NAME; and unfortunately I am still getting the same issue – jublikon Jun 02 '16 at 13:38
  • @Mauker I can loop somehow a few times without a problem and then it seems to crash on db.close() – jublikon Jun 02 '16 at 13:39
  • Possible duplicate of [Error: Make sure the Cursor is initialized correctly before accessing data from it?](http://stackoverflow.com/questions/23178441/error-make-sure-the-cursor-is-initialized-correctly-before-accessing-data-from) – Bharatesh Jun 02 '16 at 13:47
  • @skadoosh I have read that post and my code is 0 based. I am getting complete rows. The app crashes in the return after some complete rows – jublikon Jun 02 '16 at 13:49
  • @jublikon no you didn't ... see viariable scopes ... – Selvin Jun 02 '16 at 14:04
  • @Selvin I start from 0 then 1 and 2. What do you mean ? And while running a piece of code that detects with cursor.getColumnCount() how many columns there are I get the same issue. Perhaps the problem is not in the variable scopes? – jublikon Jun 02 '16 at 14:07
  • you have class field QUERY and local variable QUERY ... obviously then field is not used ... just post the full logcat log, code from the method that you get an exception only and point the line where are you getting an exception ... now is hard to resolve your problem ... **sill i bet that you have only 2 columns in the table** – Selvin Jun 02 '16 at 14:10
  • @Selvin I have updated my question – jublikon Jun 02 '16 at 14:26
  • replace `Log.e("Database Problem", e + "");` with `e.printStackTrace()` ... without it we don't know where error occurs ... also `06-02 15:05:10.224` and `06-02 15:05:10.249` ? 25 milis between errors? – Selvin Jun 02 '16 at 14:29
  • when I replace Log.e with e.printStackTrace() I do not get any output. The app does not crash but also does not get any data – jublikon Jun 02 '16 at 14:51

0 Answers0