Kotlin - IndexOutOfBoundsException when converting it from Java

Currently I'm trying to learn making games in Kotlin and I was converting the java code to Kotlin and everything is working except the below problems.

//Missile.kt

class Missile(context: Context) {

    internal var x: Int = 0
    internal var y: Int = 0
    internal var mVelocity: Int = 0
    internal var missile: Bitmap = BitmapFactory.decodeResource(context.resources, R.drawable.missile)

    val missileWidth: Int
        get() = missile.width
    val missileHeight: Int
        get() = missile.height

    init {
        x = GameView.dWidth / 2 - missileWidth / 2
        y = GameView.dHeight - GameView.tankHeight - missileHeight / 2
        mVelocity = 50
    }
}


//In Kotlin

for (i in missiles.indices) {

    if (missiles[i].y >- missiles[i].missileHeight) {

        missiles[i].y -= missiles[i].mVelocity

        canvas.drawBitmap(missiles[i].missile, missiles[i].x.toFloat(), missiles[i].y.toFloat(), null)

        if (missiles[i].x >= planes[0].planeX && missiles[i].x + missiles[i].missileWidth <= planes[0].planeX + planes[0].width && missiles[i].y >= planes[0].planeY &&

                missiles[i].y <= planes[0].planeY + planes[0].height) {

            val explosion = Explosion(context)

            explosion.explosionX = planes[0].planeX + planes[0].width / 2 - explosion.explosionWidth / 2

            explosion.explosionY = planes[0].planeY + planes[0].height / 2 - explosion.explosionHeight / 2

            explosions.add(explosion)

            planes[0].resetPosition()

            count++

            missiles.removeAt(i)

            if (point != 0) {

                sp.play(point, 1f, 1f, 0, 0, 1f)

            }



//In Java

for (int i = 0; i < missiles.size(); i++) {

    if (missiles.get(i).y > -missiles.get(i).getMissileHeight()) {

        missiles.get(i).y -= missiles.get(i).mVelocity;

        canvas.drawBitmap(missiles.get(i).missile, missiles.get(i).x, missiles.get(i).y, null);

        if (missiles.get(i).x >= planes.get(0).planeX && (missiles.get(i).x + missiles.get(i).getMissileWidth())

                <= (planes.get(0).planeX + planes.get(0).getWidth()) && missiles.get(i).y >= planes.get(0).planeY &&

                missiles.get(i).y <= (planes.get(0).planeY + planes.get(0).getHeight())) {

            Explosion explosion = new Explosion(context);

            explosion.explosionX = planes.get(0).planeX + planes.get(0).getWidth()/2 - explosion.getExplosionWidth()/2;

            explosion.explosionY = planes.get(0).planeY + planes.get(0).getHeight()/2 - explosion.getExplosionHeight()/2;

            explosions.add(explosion);

            planes.get(0).resetPosition();

            count++;

            missiles.remove(i);

            if(point != 0){

                sp.play(point, 1, 1, 0, 0, 1);

            }

The part where causing is below.

java.lang.IndexOutOfBoundsException: Index: 1, Size: 1

java.lang.IndexOutOfBoundsException: Index: 2, Size: 2



if (missiles[i].y >- missiles[i].missileHeight) {

So, the situation is if I press the cannon two times, it will cause the java.lang.IndexOutOfBoundsException: Index: 1, Size: 1

and if I press the cannon three times, it will cause the java.lang.IndexOutOfBoundsException: Index: 2, Size: 2.

If I only press the cannon one time, there will be no crash.

I would love to get possible suggestion about how to fix the problems.

728x90

1 Answers Kotlin - IndexOutOfBoundsException when converting it from Java

I believe it's because you are removing elements causing your index to be incorrect. If you are removing elements from a list, you should use Iterator to iterate the list.

Iterator itr = missiles.iterator();
while(itr.hasNext()){
    Object missile = itr.next(); 
    itr.remove();
}

the remove will also remove it from your missiles list.

4 months ago