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);
}
}
When I run the app the first time, after deleting it from the storage before I get:
|| 1 || CNC || Metallverarbeitung