FATAL EXCEPTION: main Process: example.myproject, PID: 2608 java.lang.RuntimeException: Unable to start activity ComponentInfo{edgedev.andelaproject/example.myproject.Activites_and_Fragments.MainActivity}: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
In MyDB.class (subclass of SQLiteOpenHelper):
Here is my Table Statement
String create_table = "CREATE TABLE my_table_of_profiles (profile_id INTEGER, username TEXT, profile TEXT, image TEXT, score TEXT ) ";
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(create_table);
}
in my DatabaseAccessObject.class:
public ArrayList<Profile> getEntriesFromDB(Context context) {
SQLiteDatabase db = new MyDB(context).getWritableDatabase();
Cursor cursor = db.query("my_table_of_profiles" , null, null, null, null, null, null);
Profile profile;
ArrayList<Profile> profiles = new ArrayList<>();
while (cursor.moveToNext()) {
/*Here is -> DatabaseAccessObject.java:69*/ int a = cursor.getInt(cursor.getColumnIndex("profile_id");
String b= cursor.getString(cursor.getColumnIndex("username");
String c= cursor.getString(cursor.getColumnIndex("profile");
String d= cursor.getString(cursor.getColumnIndex("image");
Double e = Double.parseDouble(cursor.getString(cursor.getColumnIndex("score");
profile = new Profile(a,b,c,d,e );
profiles.add(profile);
}
cursor.close();
db.close();
return profiles;
}
public boolean storeProfiles (Context context, ArrayList<Profile> profile) {
SQLiteDatabase db = new MyDB(context).getWritableDatabase();
db.beginTransaction();
for(Profile freshProfile : profile){
ContentValues cv = new ContentValues();
cv.put("id", freshProfile.getProfile_id());
cv.put("username", freshProfile.getProfile_username());
cv.put("profile", freshProfile.getProfile_url());
cv.put("image", freshProfile.getImage_url());
cv.put("score", freshProfile.getScore());
long result = db.insert("my_table_of_profiles", null, cv);
if (result < 0) {
return false;
}
}
db.setTransactionSuccessful();
db.endTransaction();
db.close();
return true;
}
Profile.class
public class Profile {
private int profile_id;
private String profile_username;
private String profile_url;
private String image_url;
private Double score;
public Profile(int profile_id, String profile_username, String profile_url, String image_url, Double score ) {
this.image_url = image_url;
this.profile_username = profile_username;
this.profile_url = profile_url;
this.profile_id = profile_id;
this.score = score;
}
public String getImage_url() {
return image_url;
}
public String profile_username() {
return profile_username;
}
public String getProfile_url() {
return profile_url;
}
public int getProfile_id() {
return profile_id;
}
public String getScore() {
return ""+round(score);
}
private double round(double value){
long factor = (long) Math.pow(10,3);
value = value * factor;
long tmp = Math.round(value);
return (double) tmp/factor;
}
}
In MainActivity.java (Inside OnCreate method), i put some dummy data:
Profile profile;
ArrayList<Profile> profiles = new ArrayList<>();
for (int i=0 ; i<20; i++){
profile = new Profile(i,"username"+i, "http:///www.url.com"+i,"imgur.com/abcdefgh", 1.234);
profiles.add(profile);
}
new DatabaseAccessObject().storePosts(this, profiles);
ArrayList<Profile> retrieved = DatabaseAccessObject.getsInstance().getEntriesFromDB(this);
// i also tried to retrieve the items from the database
//This is where i got an Error
an Excerpt of the Error i got :
Caused by: java.lang.IllegalStateException: Couldn't read row 0, col -1 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 edgedev.andelaproject.Database.DatabaseAccessObject.getEntriesFromDB(DatabaseAccessObject.java:69) at edgedev.andelaproject.Database.DatabaseAccessObject.storePosts(DatabaseAccessObject.java:26) at edgedev.andelaproject.Activites_and_Fragments.MainActivity.onCreate(MainActivity.java:71) at android.app.Activity.performCreate(Activity.java:6237) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)