java.lang.IndexOutOfBoundsException: Invalid index 13, size is 13

jquery404 picture jquery404 · Aug 9, 2013 · Viewed 7.9k times · Source

Im getting some weird error which crashing my android app. Its a quiz app. So when user answer 2/3 question correctly then click the next button this it crash. and show index 13 error. But i couldn't figure out where to fix/ look for. here is my code snippet.

public Vector<Sprite> defaultTile;

private void GameResults()
{
    if(result.equals("right"))      
    {           
        GameOver();
        Log.e("Gaa", "Right Here ->");
    }
}

private void GameOver() {
{
    for (int i = 0; i < defaultTile.size(); i++) {
        defaultTile.get(i).setVisible(false);               
    }
    for (int i = 0; i < defaultTile.size(); i++) 
    {
        unregisterTouchArea(defaultTile.get(i));                
    }
    questionText.detachSelf();   
}   

@Override
public boolean onAreaTouched(TouchEvent event, ITouchArea area, float posX,
        float posY) {

    if(event.isActionUp())
    {
        if(area instanceof Sprite)
        {
            Sprite sprite = (Sprite)area;
            int userData = (Integer) sprite.getUserData();
            switch(userData) 
            {
            case BTN_NEXT:                              
                if(gameState.equals("alpha") && tickBg.isVisible())
                {                       
                    countdown.cancel();                         
                    GameResults();  
                }           

                break;
            }
        }
    }
}   

Log cat details is like this

08-09 13:30:50.246: W/dalvikvm(919): threadid=12: thread exiting with uncaught exception (group=0x409c01f8)
08-09 13:30:50.276: E/AndroidRuntime(919): FATAL EXCEPTION: GLThread
08-09 13:30:50.276: E/AndroidRuntime(919): java.lang.IndexOutOfBoundsException: Invalid index 9, size is 9
08-09 13:30:50.276: E/AndroidRuntime(919):  at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
08-09 13:30:50.276: E/AndroidRuntime(919):  at java.util.ArrayList.get(ArrayList.java:304)
08-09 13:30:50.276: E/AndroidRuntime(919):  at org.anddev.andengine.entity.Entity.onManagedDrawChildren(Entity.java:1008)
08-09 13:30:50.276: E/AndroidRuntime(919):  at org.anddev.andengine.entity.Entity.onDrawChildren(Entity.java:1000)
08-09 13:30:50.276: E/AndroidRuntime(919):  at org.anddev.andengine.entity.Entity.onManagedDraw(Entity.java:993)
08-09 13:30:50.276: E/AndroidRuntime(919):  at org.anddev.andengine.entity.shape.Shape.onManagedDraw(Shape.java:120)
08-09 13:30:50.276: E/AndroidRuntime(919):  at org.anddev.andengine.entity.Entity.onDraw(Entity.java:875)
08-09 13:30:50.276: E/AndroidRuntime(919):  at org.anddev.andengine.entity.Entity.onManagedDrawChildren(Entity.java:1008)
08-09 13:30:50.276: E/AndroidRuntime(919):  at org.anddev.andengine.entity.Entity.onDrawChildren(Entity.java:1000)
08-09 13:30:50.276: E/AndroidRuntime(919):  at org.anddev.andengine.entity.Entity.onManagedDraw(Entity.java:993)
08-09 13:30:50.276: E/AndroidRuntime(919):  at org.anddev.andengine.entity.scene.Scene.onManagedDraw(Scene.java:233)
08-09 13:30:50.276: E/AndroidRuntime(919):  at org.anddev.andengine.entity.Entity.onDraw(Entity.java:875)
08-09 13:30:50.276: E/AndroidRuntime(919):  at org.anddev.andengine.engine.Engine.onDrawScene(Engine.java:517)
08-09 13:30:50.276: E/AndroidRuntime(919):  at org.anddev.andengine.engine.Engine.onDrawFrame(Engine.java:509)
08-09 13:30:50.276: E/AndroidRuntime(919):  at org.anddev.andengine.opengl.view.RenderSurfaceView$Renderer.onDrawFrame(RenderSurfaceView.java:154)
08-09 13:30:50.276: E/AndroidRuntime(919):  at org.anddev.andengine.opengl.view.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:617)
08-09 13:30:50.276: E/AndroidRuntime(919):  at org.anddev.andengine.opengl.view.GLSurfaceView$GLThread.run(GLSurfaceView.java:549)

UPDATE

Here is my full class file http://jquery404.com/file/GameScene.txt

Answer

Robadob picture Robadob · Aug 9, 2013

In programming indexes often start at 0, so if you have 9 items, the highest index would be 8.

The actual error is being thrown from some code within the library you are using

org.anddev.andengine.entity.Entity.onManagedDrawChildren(Entity.java:1008)

It is likely that you are changing the list in a separate thread whilst the library is also interacting with the list.


From the gcode project;

    public void onManagedDrawChildren(final Camera pCamera) {
            final ArrayList<IEntity> children = this.mChildren;
            final int childCount = children.size();
            for(int i = 0; i < childCount; i++) {
                    children.get(i).onDraw(pCamera);
            }
    }

As this is running in a separate thread, it is likely that you are removing an object from the children ArrayList while the loop is iterating. To fix this you should call your changes to the children ArrayList like jmr499485 explains in his answer.

java.lang.IndexOutOfBoundsException: Invalid index 13, size is 13

The only item in your code I can see that would be causing this is the statement questionText.detachSelf(); which you have used in many places. You should instead use;

runOnUpdateThread(new Runnable() {
@Override
public void run() {
    questionText.detachSelf();
}
});