Fluppe
Fluppe

Reputation: 497

Android android.database.CursorIndexOutOfBoundsException: Index x requested, with a size of x

I'm trying to fill up 10 textviews with data from my HighscoreDB. Every time my application's game ends, the user is asked to store their score in the database, along with a name. Then, I fill 10 textviews (actually 20, but there are 10 rows with name and score.). This used to work (when there were a few highscore records in HighscoreDB), but ever since I created a new emulator and thereby deleting all the highscore rows, I keep getting the "android.database.CursorIndexOutOfBoundsException: Index x requested, with a size of x" error. The x represents how many rows I currently have, for instance if I have 5 highscore rows in the DB, the error will be "Index 5 requested, with a size of 5" (found that out by trying out new ways to fix my problem but it remained, and I noticed that the index also increases). Can anyone point out what I'm doing wrong or how tell me how to fix my code?

This is the code in my HighscoreActivity in which the highscores are put in the textviews:

                   db = (new HighscoreDB(this)).getWritableDatabase();
cursor = db.rawQuery("SELECT _id, name, score FROM highscore ORDER BY score DESC",  null);
                   cursor.moveToFirst();
             for (int i = 0; i < 10; i++) {



            TableRow row = (TableRow)mHighscoreTable.getChildAt(i+1);
            TextView tvName = (TextView)row.getChildAt(1);

            tvName.setText(cursor.getString(1));
            TextView tvScore = (TextView)row.getChildAt(2);

            tvScore.setText(Integer.toString(cursor.getInt(2)));


            cursor.moveToNext();



    }

The line "for (int i = 0; i < 10; i++) {" is because I only want to show 10 highscores at a time in the 10 rows of textviews.

This is how the table is made:

private static final String SCORE_TABLE_CREATE = "CREATE TABLE "
                                                 + SCORE_TABLE_NAME
                                                 + " (_id INTEGER PRIMARY KEY autoincrement, "
                                                 + "name TEXT NOT NULL, score INTEGER  NOT NULL)";

This was my latest error:

05-25 17:03:16.786: E/AndroidRuntime(780): FATAL EXCEPTION: main
05-25 17:03:16.786: E/AndroidRuntime(780): java.lang.RuntimeException: Unable to start activity ComponentInfo{be.michiel.test/be.michiel.test.HighscoreActivity}: android.database.CursorIndexOutOfBoundsException: Index 5 requested, with a size of 5
05-25 17:03:16.786: E/AndroidRuntime(780):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
05-25 17:03:16.786: E/AndroidRuntime(780):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
05-25 17:03:16.786: E/AndroidRuntime(780):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
05-25 17:03:16.786: E/AndroidRuntime(780):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
05-25 17:03:16.786: E/AndroidRuntime(780):  at android.os.Handler.dispatchMessage(Handler.java:99)
05-25 17:03:16.786: E/AndroidRuntime(780):  at android.os.Looper.loop(Looper.java:123)
05-25 17:03:16.786: E/AndroidRuntime(780):  at android.app.ActivityThread.main(ActivityThread.java:3683)
05-25 17:03:16.786: E/AndroidRuntime(780):  at java.lang.reflect.Method.invokeNative(Native Method)
05-25 17:03:16.786: E/AndroidRuntime(780):  at java.lang.reflect.Method.invoke(Method.java:507)
05-25 17:03:16.786: E/AndroidRuntime(780):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
05-25 17:03:16.786: E/AndroidRuntime(780):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-25 17:03:16.786: E/AndroidRuntime(780):  at dalvik.system.NativeStart.main(Native Method)
05-25 17:03:16.786: E/AndroidRuntime(780): Caused by: android.database.CursorIndexOutOfBoundsException: Index 5 requested, with a size of 5
05-25 17:03:16.786: E/AndroidRuntime(780):  at android.database.AbstractCursor.checkPosition(AbstractCursor.java:580)
05-25 17:03:16.786: E/AndroidRuntime(780):  at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:214)
05-25 17:03:16.786: E/AndroidRuntime(780):  at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:41)
05-25 17:03:16.786: E/AndroidRuntime(780):  at be.michiel.test.HighscoreActivity.onCreate(HighscoreActivity.java:79)
05-25 17:03:16.786: E/AndroidRuntime(780):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-25 17:03:16.786: E/AndroidRuntime(780):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
05-25 17:03:16.786: E/AndroidRuntime(780):  ... 11 more

Please help me find my mistake. I've been trying to find out for a long time now. Thanks in advance!

Upvotes: 3

Views: 5057

Answers (2)

Shraddha
Shraddha

Reputation: 1052

You should never do this without checking whether the next record is available or not. Try changing your code like below :

db = (new HighscoreDB(this)).getWritableDatabase();
cur = db.rawQuery("SELECT _id, name, score FROM highscore ORDER BY score DESC",  null);
int noOfScorer=0;
cur.moveToFirst();
    while ((!cur.isAfterLast())&&noOfScorer<11) 
    {
        noOfScorer++;
        TableRow row = (TableRow)mHighscoreTable.getChildAt(noOfScorer);
        TextView tvName = (TextView)row.getChildAt(1);
        TextView tvScore = (TextView)row.getChildAt(2);

        tvName.setText(cur.getString(1));
        tvScore.setText(Integer.toString(cur.getInt(2)));

        cur.moveToNext();
    }

Hope this helps you.

Upvotes: 5

Xnay
Xnay

Reputation: 11

You get the error because you're trying to access the 11th TableRow child, which doesn't exist.

Replace
TableRow row = (TableRow)mHighscoreTable.getChildAt(i+1);
with
TableRow row = (TableRow)mHighscoreTable.getChildAt(i);


Also, your indexes are getting bigger because you set the ID property to "auto increment".

Another problem with the code is that you're calling cursor.moveToNext(); on the 10th item. You could modify your "for" statement so it looks like this:

for (int i = 0; i < 10 && !cursor.isAfterLast(); i++) {

Upvotes: 1

Related Questions