-2

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

Nizarhdt
  • 23
  • 3
  • 3
    "i got error" - Okay, what's the error? – Paul Bellora Oct 09 '13 at 22:24
  • i got this messages : Exception in thread "Thread-3" java.lang.NullPointerException – Nizarhdt Oct 09 '13 at 22:29
  • 1
    The exception stack trace should show what line of code caused the NPE. Please identify what line of code in your posted code caused the exception. Also, what is `r` in the `checkCollision` method? – Ted Hopp Oct 09 '13 at 22:32
  • this line `if(r.intersects(e.r)){` caused the exception. r is rectangle of bullets. its running normaly before enemy pass away from screen `enemy.remove(i);`. @Ted-Hopp – Nizarhdt Oct 09 '13 at 22:39
  • If that line is throwing a NPE, then either `r` or `e` is `null` when that line executes. (Or perhaps `e.r`, if the NPE is being thrown from within the call to `intersects`.) – Ted Hopp Oct 09 '13 at 23:51

1 Answers1

0

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.

RevMuun
  • 58
  • 1
  • 5
  • thank you guys, its caused `r=null;` in my code. now my app running normally after delete it. thanks. – Nizarhdt Oct 09 '13 at 23:05