gusain.jr
gusain.jr

Reputation: 76

Cursor Index Out Of Bounds Exception in android

There is a score Board in My game..There is no score still it shows CursoreIndexOutOfBoundsException how am i suppose to solve this error..there are many solution but it didnt work for me below is my code

public List<Rank> getRanking() {
    List<Rank> listRanking = new ArrayList<>();
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor c = db.rawQuery("SELECT * FROM Rank Order By Score DESC LIMIT 10;", null);
    try {

        if (c == null)
        {return null;}

        c.moveToNext();
        do {
            int Id = c.getInt(c.getColumnIndex("ID"));
            int Score = c.getInt(c.getColumnIndex("Score"));


            Rank ranking = new Rank(Id, Score);
            listRanking.add(ranking);
        } while (c.moveToNext());

    } catch (Exception e) {
        e.printStackTrace();
    }
    finally {
        c.close();
        db.close();
    }
    return listRanking;

}

Can anyone help me with a solution to this problem.Any help or suggestion is appreciated.Thank you.

Upvotes: 0

Views: 300

Answers (3)

VikasGoyal
VikasGoyal

Reputation: 3376

Please try with this it will work:-

public List<Rank> getRanking() {
    List<Rank> listRanking = new ArrayList<>();
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor c = db.rawQuery("SELECT * FROM Rank Order By Score DESC LIMIT 10;", null);
    try {

        if (c == null)
        {return null;}

        while (c.moveToNext()) {
            int index = c.getColumnIndex("ID");
            int Id = 0;
            if (-1 != index) {
                Id = c.getInt(index);
            }
            index = c.getColumnIndex("Score");
            int Score = 0;
            if (-1 != index) {
                Score = c.getInt(index1);
            }


            Rank ranking = new Rank(Id, Score);
            listRanking.add(ranking);
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
    finally {
        if (null != c) {
            c.close();
        }
        db.close();
    }
    return listRanking;

}

Upvotes: 1

BlackHatSamurai
BlackHatSamurai

Reputation: 23513

The issue is that you are moving to next, when there is no next. You need to check and make sure there is a next to go to. Also, get rid of the first c.moveToNext(). You don't need it. It makes you skip over the first value. Just do:

       try {

        if (c == null){
             return null;
        }
        do {
            int Id = c.getInt(c.getColumnIndex("ID"));
            int Score = c.getInt(c.getColumnIndex("Score"));


            Rank ranking = new Rank(Id, Score);
            listRanking.add(ranking);
            if(c.moveToNext() == null){
                 break;
             }
        } while (c.moveToNext());

Upvotes: 0

aaroncio
aaroncio

Reputation: 307

Try changing the first c.moveToNext() to c.moveToFirst():

public List<Rank> getRanking() {
    List<Rank> listRanking = new ArrayList<>();
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor c = db.rawQuery("SELECT * FROM Rank Order By Score DESC LIMIT 10;", null);
    try {

        if (c == null)
        {return null;}
//changed moveToNext() to moveToFirst()
    if (c.moveToFirst()) {
                do {
                int Id = c.getInt(c.getColumnIndex("ID"));
                int Score = c.getInt(c.getColumnIndex("Score"));


                Rank ranking = new Rank(Id, Score);
                listRanking.add(ranking);
                } while (c.moveToNext());
            }
   } catch (Exception e) {
        e.printStackTrace();
    }
    finally {
        c.close();
        db.close();
    }
    return listRanking;

}

Upvotes: 0

Related Questions