I was starting my app to make the connection and return the list with the data of the object and throws me that error some solution to be able to continue with my app?
2018-11-11 22: 04: 55.122 4942-4942 /? E / AndroidRuntime: FATAL EXCEPTION: main Process: com.example.proyecto.pizzaappproyect, PID: 4942 java.lang.RuntimeException: Unable to start activity ComponentInfo {com.example.proyecto.pizzaappproyect / com.example.proyecto.pizzaappproyect.MainActivity}: java.lang.IllegalStateException: Could not read row 0, col 5 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it. at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2668) at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2729) at android.app.ActivityThread.-wrap12 (ActivityThread.java) at android.app.ActivityThread $ H.handleMessage (ActivityThread.java:1480) at android.os.Handler.dispatchMessage (Handler.java:102) at android.os.Looper.loop (Looper.java:154) at android.app.ActivityThread.main (ActivityThread.java:6169) at java.lang.reflect.Method.invoke (Native Method) at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:891) at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:781) Caused by: java.lang.IllegalStateException: Could not read row 0, col 5 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it. at android.database.CursorWindow.nativeGetLong (Native Method) at android.database.CursorWindow.getLong (CursorWindow.java:511) at android.database.CursorWindow.getInt (CursorWindow.java:578) at android.database.AbstractWindowedCursor.getInt (AbstractWindowedCursor.java:69) at com.example.proyecto.pizzaappproyect.DB.CRUD.pizzaList (CRUD.java:108) at com.example.proyecto.pizzaappproyect.MainActivity.onCreate (MainActivity.java:59) at android.app.Activity.performCreate (Activity.java:6692) at android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1118) at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2621) at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2729) at android.app.ActivityThread.-wrap12 (ActivityThread.java) at android.app.ActivityThread $ H.handleMessage (ActivityThread.java:1480) at android.os.Handler.dispatchMessage (Handler.java:102) at android.os.Looper.loop (Looper.java:154) at android.app.ActivityThread.main (ActivityThread.java:6169) at java.lang.reflect.Method.invoke (Native Method) at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:891) at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:781)
public List<Pizza> pizzaList() {
List<Pizza> list = new ArrayList<>();
db = helper.getReadableDatabase();
String sql = "select * from " + ConexionHelper.TABLE;
Cursor cursor = db.rawQuery(sql, null);
while (cursor.moveToNext()) {
Pizza p = new Pizza();
p.id = cursor.getString(0);
p.nombre = cursor.getString(1);
p.foto1 = cursor.getInt(2);
p.igrediente1=cursor.getInt(3);
p.igrediente2=cursor.getInt(4);
p.igrediente3=cursor.getInt(5); // esta es la que arroja el error
p.igrediente4=cursor.getInt(6);
p.calificacion=cursor.getInt(7);
p.descripcion = cursor.getString(8);
p.precio = cursor.getInt(9);
list.add(p);
}
db.close();
return list;
}
CRUD
public class CRUD {
private ConexionHelper helper;
private ContentValues values;
private SQLiteDatabase db;
public CRUD(Context context) {
helper = new ConexionHelper(context);
values = new ContentValues();
}
public void insert(Pizza p) {
db = helper.getWritableDatabase();
values.clear();
values.put(ConexionHelper.NOMBRE, p.nombre);
values.put(ConexionHelper.FOTO, p.foto1);
values.put(ConexionHelper.INGREDIENTE1, p.igrediente1);
values.put(ConexionHelper.INGREDIENTE2, p.igrediente2);
values.put(ConexionHelper.INGREDIENTE3, p.igrediente3);
values.put(ConexionHelper.INGREDIENTE4, p.igrediente4);
values.put(ConexionHelper.CALIFICACION, p.calificacion);
values.put(ConexionHelper.DESCRIPCION, p.descripcion);
values.put(ConexionHelper.PRECIO, p.precio);
db.insert(ConexionHelper.TABLE, null, values);
db.close();
}
public void delete(String id) {
String pk = id + "";
db = helper.getWritableDatabase();
db.delete(ConexionHelper.TABLE,
ConexionHelper.ID + "=?",
new String[]{pk});
db.close();
}
public void update(Pizza p) {
db = helper.getWritableDatabase();
values.clear();
values.put(ConexionHelper.NOMBRE, p.nombre);
values.put(ConexionHelper.FOTO, p.foto1);
values.put(ConexionHelper.INGREDIENTE1, p.igrediente1);
values.put(ConexionHelper.INGREDIENTE2, p.igrediente2);
values.put(ConexionHelper.INGREDIENTE3, p.igrediente3);
values.put(ConexionHelper.INGREDIENTE4, p.igrediente4);
values.put(ConexionHelper.CALIFICACION, p.calificacion);
values.put(ConexionHelper.DESCRIPCION, p.descripcion);
values.put(ConexionHelper.PRECIO, p.precio);
String pk = p.id + "";//String.valueOf(m.id);
db.update(ConexionHelper.TABLE,
values,
ConexionHelper.ID + "=?",
new String[]{pk});
db.close();
}
public Pizza find(String id) {
Pizza p = new Pizza();
db = helper.getReadableDatabase();
String sql = "select * from " + ConexionHelper.TABLE + " where " + ConexionHelper.ID + "=?";
String pk = id + "";
Cursor cursor = db.rawQuery(sql, new String[]{pk});
if (cursor.moveToNext()) {
p.id = cursor.getString(0);
p.nombre = cursor.getString(1);
p.foto1 = cursor.getInt(2);
p.igrediente1 = cursor.getInt(3);
p.igrediente2 = cursor.getInt(4);
p.igrediente3 = cursor.getInt(5);
p.igrediente4 = cursor.getInt(6);
p.calificacion = cursor.getInt(7);
p.descripcion = cursor.getString(8);
p.precio = cursor.getInt(9);
}
db.close();
return p;
}
public List<Pizza> pizzaList() {
List<Pizza> list = new ArrayList<>();
db = helper.getReadableDatabase();
String sql = "select * from " + ConexionHelper.TABLE;
Cursor cursor = db.rawQuery(sql, null);
while (cursor.moveToNext()) {
Pizza p = new Pizza();
p.id = cursor.getString(0);
p.nombre = cursor.getString(1);
p.foto1 = cursor.getInt(2);
p.igrediente1 = cursor.getInt(3);
p.igrediente2 = cursor.getInt(4);
p.igrediente3 = cursor.getInt(5);
p.igrediente4 = cursor.getInt(6);
p.calificacion = cursor.getInt(7);
p.descripcion = cursor.getString(8);
p.precio = cursor.getInt(9);
list.add(p);
}
db.close();
return list;
}
}
HELPER
public class ConexionHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "data";
private static final int VERSION = 1;
public static final String TABLE = "pizza";
public static final String ID = "id";
public static final String NOMBRE = "nombre";
public static final String FOTO = "foto1";
public static final String INGREDIENTE1 = "igrediente1";
public static final String INGREDIENTE2 = "igrediente2";
public static final String INGREDIENTE3 = "igrediente3";
public static final String INGREDIENTE4 = "igrediente4";
public static final String CALIFICACION = "calificacion";
public static final String DESCRIPCION = "descripcion";
public static final String PRECIO = "precio";
public ConexionHelper(Context context) {
super(context, DATABASE_NAME, null, VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String script = "";
script += "create table " + TABLE + "(";
script += ID + " integer primary key autoincrement,";
script += NOMBRE + " text,";
script += FOTO + " text,";
script += INGREDIENTE1 + " text,";
script += INGREDIENTE2 + " text,";
script += INGREDIENTE3 + " text,";
script += INGREDIENTE4 + " text,";
script += CALIFICACION + " integer";
script += DESCRIPCION + " text,";
script += PRECIO + " integer";
script += ");";
db.execSQL(script);
db.execSQL("insert into " + TABLE + " values( 'Española',"+ R.drawable.pizza_espanola+" , "+R.drawable.espa_ola1+" , "+R.drawable.espa_ola2+" , "+R.drawable.espa_ola3+" , "+R.drawable.espa_ola4+" ,5, 'ÑAMI ÑAMI',7500);");
db.execSQL("insert into " + TABLE + " values( 'Todas Las Carnes'," + R.drawable.todascarne + "," + R.drawable.todas1 + "," + R.drawable.todas2 + " ," + R.drawable.todas3 + " ," + R.drawable.todas4 + " ,4, 'DELICHIUSS',5500);");
db.execSQL("insert into " + TABLE + " values( 'Vegetariana'," + R.drawable.vegetariana + "," + R.drawable.veg1 + "," + R.drawable.veg2 + " ," + R.drawable.veg3 + " ," + R.drawable.veg4 + " ,3, 'KAKAKAK',1500);");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("drop table " + TABLE);
onCreate(db);
}
}