-2

I am not sure why I am getting this error:

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

I am getting it for these lines in the sqlite:

db_d.setIsFollowingType(c.getInt(c.getColumnIndex(KEY_IS_FOLLOWING_TYPE)));
db_d.setIsFollowerType(c.getInt(c.getColumnIndex(KEY_IS_FOLLOWER_TYPE)));

If I comment them out it works, this is the whole sqlite method:

public List<DatabaseUser> getUsers(int type) {

        String whereClause = KEY_IS_FOLLOWING_TYPE + " = ? ";
        if(type == 1) {

            whereClause = KEY_IS_FOLLOWER_TYPE + " = ? ";
        }
        List<DatabaseUser> getUsers = new ArrayList<DatabaseUser>();
        SQLiteDatabase db = this.getReadableDatabase();
        String[] whereArgs = new String[]{
                "1"
        };
        String orderBy = KEY_ID + " DESC";

        Cursor c = db.query(TABLE_USERS, null, whereClause, whereArgs, null, null, orderBy);

        if (c.moveToFirst()) {
            do {
                DatabaseUser db_d = new DatabaseUser();

                db_d.setUserId(c.getString(c.getColumnIndex(KEY_USER_ID)));
                db_d.setPicture(c.getString(c.getColumnIndex(KEY_PICTURE)));
                db_d.setUsername(c.getString(c.getColumnIndex(KEY_USERNAME)));

                db_d.setFollowersCount(c.getInt(c.getColumnIndex(KEY_FOLLOWERS_COUNT)));
                db_d.setFollowingCount(c.getInt(c.getColumnIndex(KEY_FOLLOWING_COUNT)));
                db_d.setTrackingCount(c.getInt(c.getColumnIndex(KEY_TRACKING_COUNT)));
                db_d.setIsFollowingType(c.getInt(c.getColumnIndex(KEY_IS_FOLLOWING_TYPE)));
                db_d.setIsFollowerType(c.getInt(c.getColumnIndex(KEY_IS_FOLLOWER_TYPE)));
                db_d.setIsChanged(c.getInt(c.getColumnIndex(KEY_IS_CHANGED)));

                db_d.setDateAdded(c.getLong(c.getColumnIndex(KEY_DATE_ADDED)));

                Log.d(Constants.DEBUG, "DB pull user product " + db_d);
                getUsers.add(db_d);
            } while (c.moveToNext());
        }

        //calling for close for recycling make sure it does not cause an error
        c.close();
        return getUsers;

    }

This is my Table Create method:

private static final String CREATE_TABLE_USERS = "CREATE TABLE "
            + TABLE_USERS + "("
            + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
            + KEY_USER_ID + " TEXT NOT NULL UNIQUE ON CONFLICT REPLACE,"
            + KEY_PICTURE + " TEXT,"
            + KEY_USERNAME + " TEXT,"
            + KEY_FOLLOWERS_COUNT + " INTEGER,"
            + KEY_FOLLOWING_COUNT + " INTEGER,"
            + KEY_TRACKING_COUNT + " INTEGER,"
            + KEY_IS_FOLLOWING_TYPE + " INTEGER,"
            + KEY_IS_FOLLOWER_TYPE + " INTEGER,"
            + KEY_IS_CHANGED + " INTEGER,"
            + KEY_DATE_ADDED + " LONG"
            + ")";

This is DatabaseUser Object:

public class DatabaseUser {



    private String userId, picture, username;
    private int followersCount, followingCount, trackCount, isChanged, isFollowingType, isFollowerType; 
    private long dateAdded;


    //TODO USER NO NEED FOR ISFOLLOWING
    public DatabaseUser() {
    }


    //TODO TESTING
    public DatabaseUser(String uId, String pic, String uname,
                        int followersC, int followingC, int trackC, int isChng, int followingtype, int followertype,
                        long dateAdd) {
        userId = uId;
        picture = pic;
        username = uname;
        followersCount = followersC;
        followingCount = followingC;
        trackCount = trackC;
        isChanged = isChng;
        isFollowingType = followingtype;
        isFollowerType = followertype;
        dateAdded = dateAdd;


    }


    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getPicture() {
        return picture;
    }

    public void setPicture(String picture) {
        this.picture = picture;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public int getFollowersCount() {
        return followersCount;
    }

    public void setFollowersCount(int followersCount) {
        this.followersCount = followersCount;
    }

    public int getFollowingCount() {
        return followingCount;
    }

    public void setFollowingCount(int followingCount) {
        this.followingCount = followingCount;
    }

    public int getTrackingCount() {
        return trackCount;
    }

    public void setTrackingCount(int trackCount) {
        this.trackCount = trackCount;
    }

    public int getIsChanged() {
        return isChanged;
    }

    public void setIsChanged(int isChanged) {
        this.isChanged = isChanged;
    }

    public int getIsFollowingType() {
        return isFollowingType;
    }

    public void setIsFollowingType(int isFollowingType) {
        this.isFollowingType = isFollowingType;
    }

    public long getDateAdded() {
        return dateAdded;
    }

    public void setDateAdded(long dateAdded) {
        this.dateAdded = dateAdded;
    }

    public int getIsFollowerType() {
        return isFollowerType;
    }

    public void setIsFollowerType(int isFollowerType) {
        this.isFollowerType = isFollowerType;
    }
}

This is where I am doing the check in the MainHome Activity where the error is occurring:

 private void testCreateUserFollowing() {


        //0 is for isType following
        DatabaseUser testUserFollowing1 = new DatabaseUser("userId1", "pictureId", "username1", 2354, 30534, 498231, 0, 1, 3, 0);
        DatabaseUser testUserFollowing2 = new DatabaseUser("userId2", "pictureId", "username2", 2750000, 30000000, 4, 0, 1, 3, 0);
        DatabaseUser testUserFollowing3 = new DatabaseUser("userId3", "pictureId", "username3", 2, 3, 4, 0, 1, 3, 0);



        DatabaseUser testUserFollower1 = new DatabaseUser("userId4", "pictureId", "username4", 2, 3, 4, 0, 3, 1, 0);
        DatabaseUser testUserFollower2 = new DatabaseUser("userId5", "pictureId", "username5", 2, 3, 4, 0, 3, 1, 0);
        DatabaseUser testUserFollower3 = new DatabaseUser("userId6", "pictureId", "username6", 2, 3, 4, 0, 3, 1, 0);

        DatabaseUser checkUsersExist = db.getSingleUser("userId3");
        if(checkUsersExist != null) {

        } else {
            db.createUser(testUserFollowing1);
            db.createUser(testUserFollowing2);
            db.createUser(testUserFollowing3);
            db.createUser(testUserFollower1);
            db.createUser(testUserFollower2);
            db.createUser(testUserFollower3);
        }


    }

Commenting out tee two fields as I mentioned above works, but I need for it to work with uncommenting it... Also, I realized other sqlite errors pop up for another row with the same error for another Table... if that helps with anything. I have no errors on the SQLITE Helper Class that contains the methods of sqlite... so unsure why this error is popping up...

UPDATE: FOR anyone interested: apparently calling deleteDatabase("dbname"); only worked, and uninstalling or clearing the data of the app did not...

Lion789
  • 4,402
  • 12
  • 58
  • 96
  • visit http://stackoverflow.com/questions/25958555/cursors-sqlite-and-queries – Bharatesh Apr 13 '16 at 05:10
  • 2
    Have you recently added the column to your `CREATE TABLE` and did not upgrade/wipe existing database files? – laalto Apr 13 '16 at 05:23
  • Yep because I reinstalled the app to clear out the old database, which is when this error started popping up, after clearing the database. Otherwise it was working fine before that so not sure, since the test create in the MainHome class check exists method has been added and I cleared the database it started to occur. – Lion789 Apr 13 '16 at 05:32
  • add your createUser method code – praj Apr 13 '16 at 06:00
  • 1
    You have to uninstall the app and reinstall it. Since you are in development phase uninstalling is enough. If you have going for version change and all then, you had to upgrade your db. – Jeevanandhan Apr 13 '16 at 06:53
  • I did that already... As I mentioned – Lion789 Apr 13 '16 at 11:27

1 Answers1

-1

First you have require to check cursor are null or not then after get data from it,like following

if(c!=null){
 if (c.moveToFirst()) {
            do {
                DatabaseUser db_d = new DatabaseUser();

                db_d.setUserId(c.getString(c.getColumnIndex(KEY_USER_ID)));
                db_d.setPicture(c.getString(c.getColumnIndex(KEY_PICTURE)));
                db_d.setUsername(c.getString(c.getColumnIndex(KEY_USERNAME)));

                db_d.setFollowersCount(c.getInt(c.getColumnIndex(KEY_FOLLOWERS_COUNT)));
                db_d.setFollowingCount(c.getInt(c.getColumnIndex(KEY_FOLLOWING_COUNT)));
                db_d.setTrackingCount(c.getInt(c.getColumnIndex(KEY_TRACKING_COUNT)));
                db_d.setIsFollowingType(c.getInt(c.getColumnIndex(KEY_IS_FOLLOWING_TYPE)));
                db_d.setIsFollowerType(c.getInt(c.getColumnIndex(KEY_IS_FOLLOWER_TYPE)));
                db_d.setIsChanged(c.getInt(c.getColumnIndex(KEY_IS_CHANGED)));

                db_d.setDateAdded(c.getLong(c.getColumnIndex(KEY_DATE_ADDED)));

                Log.d(Constants.DEBUG, "DB pull user product " + db_d);
                getUsers.add(db_d);
            } while (c.moveToNext());
        }
}
Dhaval Solanki
  • 4,589
  • 1
  • 23
  • 39
  • I tried this method it does not work, c is not null it is that the record does not exist for those to columns which is giving me the error – Lion789 Apr 13 '16 at 11:28
  • Ok, Then please confirm all the key of column which you are use to get data are same with column name returning. – Dhaval Solanki Apr 13 '16 at 11:35