Search code examples
javaarraylistcollision-detectioncollisionrectangles

Two ArrayList in run() method Collision Detection


i have problem with checking collision with 2 arraylist that updating in run() method. here is my code in run() method:

 ArrayList enemy = addenemy.getEnemy();
        for (int i = 0; i < enemy.size(); i++) {
            Enemy p = (Enemy) enemy.get(i);
            if (p.isVisible() == true) {
                p.update();                    
            } else {
                enemy.remove(i);

            }}

ArrayList bullets = CharS.getBullets();
        for (int i = 0; i < bullets.size(); i++) {
            Bullets p = (Bullets) bullets.get(i);
            if (p.isVisible() == true) {
                p.update();
            } else {
                bullets.remove(i);

            }}

and here is my collision code in bullet class.

private void checkCollision() {
    ArrayList enemy = Game.getEnemy();
        for (int i = 0; i < enemy.size(); i++) {
            Enemy e = (Enemy) enemy.get(i);

        if(r.intersects(e.r)){
        visible = false;

        System.out.println("SHOTED");

        }}}

i got error when when enemy object removed(out of screen). how can i resolve this? thanks


Solution

  • Are you definining r as anything? I don't see anywhere where r is defined. This would cause your NullPointerException error.

    Check the stack trace for the line number that the error is occurring on.