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

2020/3/28

NullPointer Exception

Starlord_20 Starlord_20

2020/3/28

#
int i = 0;
    int acs;
    Asteroid(int a) {
        if(a > 2) acs = a;
        else acs = 2;
    }

    public void act() 
    {

        if((this.getX() < 5) || (this.getX() > getWorld().getWidth()-5) || (this.getY() == 399)) {
            getWorld().removeObject(this);
        }

        if(i == 0) {
            this.turnTowards(Greenfoot.getRandomNumber(getWorld().getWidth()),getWorld().getHeight()); i++;
        }
        this.move(acs/2);

    }   
Why does that give a NullPointer Exception?
danpost danpost

2020/3/28

#
Starlord_20 wrote...
<< Code Omitted >> Why does that give a NullPointer Exception?
If line 12 executes, line 16 cannot execute. Cannot get the width or height of a null value (returned by getWorld -- line 16). Just move lines 11 thru 13 to after line 18.
Starlord_20 Starlord_20

2020/3/28

#
oh...fixed. thank you!
You need to login to post a reply.