-2

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)

EdgeDev
  • 2,376
  • 2
  • 20
  • 37

2 Answers2

2
int a = cursor.getInt(cursor.getColumnIndex("id");

You don't have a column id.

It should be profile_id :

int a = cursor.getInt(cursor.getColumnIndex("profile_id");

Don't forget to reinstall the app after you make this change.

Abhishek Jain
  • 3,562
  • 2
  • 26
  • 44
  • I have changed it to, `String create_table = "CREATE TABLE my_table_of_profiles (profile_id INTEGER, username TEXT, profile TEXT, image TEXT, score TEXT ) "`. `int a = cursor.getInt(cursor.getColumnIndex("profile_id")` still getting the same error – EdgeDev Mar 07 '17 at 17:42
  • @Micklo_Nerd Did you uninstall and reinstall the app? – Abhishek Jain Mar 07 '17 at 17:44
  • ooh! lemme try that now! – EdgeDev Mar 07 '17 at 17:45
  • I uninstalled the app and reinstalled again, same error occured – EdgeDev Mar 07 '17 at 17:48
  • `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 example.myproject.Database.DatabaseAccessObject.getEntriesFromDB(DatabaseAccessObject.java:69)` – EdgeDev Mar 07 '17 at 17:49
  • @Micklo_Nerd Is the error still happening at the same line? – Abhishek Jain Mar 07 '17 at 17:56
0

Caused by: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing

happened to me in an Android studio/Kotlin project today

the problem was

I was inserting

col1 col2 col3 col4 col5

but in other class I had an arrayOf (col1 col2 col3) only (same database) adding col4 col5 solved the problem I know is not a direct answer just only to ilustrate the error itself that is not related with windowcursor and or .movetofirst

hope this help a little

MarkT
  • 378
  • 2
  • 7