This site requires JavaScript, please enable it in your browser!
Greenfoot back
realsaifalrawie
realsaifalrawie wrote ...

2020/2/3

NullPointerException

realsaifalrawie realsaifalrawie

2020/2/3

#
Hi, when the tie fighter collides with an asteroid the tie fighter should be removed. now i have an enemy (TieEnemy) and i have the same if-statement. but when it collides with the tieenemy i have the nullpointerexception problem Code of the tie fighter:
if (isTouching(Asteroid2.class)){
            ((TieWorld)getWorld()).gameOver();
        }
        else
        if (isTouching(TieEnemy.class)){
            ((TieWorld)getWorld()).gameOver();
            getWorld().removeObject(tieenemy); //here is the NullPointerException problem
        }
code of "public void gameOver" in the main world:
public void gameOver (){
        backgroundMusic.stop();
        stopped();
        gameover.play();
        int x = tie.getX();
        int y = tie.getY();
        addObject(new KleineExplosion(), x, y);
        removeObject(tie);
        addObject(new GameOverScreen(), 450, 300);
    }
thanks
realsaifalrawie realsaifalrawie

2020/2/3

#
line 7 of the tie fighter code
danpost danpost

2020/2/3

#
realsaifalrawie wrote...
line 7 of the tie fighter code
I will presume that nothing is ever assigned to tieenemy (that which is to be removed). At any rate, you can instead use the removeTouching method to remove that actor.
You need to login to post a reply.