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

2017/3/20

How to avoid nullPointerException

wueschn wueschn

2017/3/20

#
I have a theoretical answer. If I code this behavier, I get a nullPointerException
public class Ants extends Actor
{

    public void act() 
    {
        this.touchingTheEdge();   
        this.turn(3);
        this.setLocation(this.getX() + 2, this.getY());

        
    }
    public void touchingTheEdge()
    {
        if(this.isAtEdge())
        {
            this.getWorld().removeObject(this);
        }

    }    
}
If I change the coding a little bit, I get no error.
public class Ants extends Actor
{

    public void act() 
    {
           
        this.turn(3);
        this.setLocation(this.getX() + 2, this.getY());
        this.touchingTheEdge();
        
    }
    public void touchingTheEdge()
    {
        if(this.isAtEdge())
        {
            this.getWorld().removeObject(this);
        }

    }    
}
It is logical for me, that if an object is removed it is not possibe to turn or to setLocation any more. If an object is removed in the end of the act-method, why does it work? After been removed it should go on turning and setLocation and get a NullpointerException too... I would be happy for an explanation or for a better solution. Thanks a lot Wueschn
Super_Hippo Super_Hippo

2017/3/20

#
On each act cycle, Greenfoot calls the act methods of the active world and each actor which is in the current world. So when you remove an object from the world, its act method won't be called anymore. (It only finishes the current act method.) Btw, you can remove every instance of "this.".
wueschn wueschn

2017/3/21

#
@Super_Hippo: thanks a lot for the explanation of an act cycle, that is logic for me now But I have to ask again: a.) Is there a possibility to ask in the act method, if an object is null or not, so that nevertheless I could use this code withourt nullPointerException
public class Ants extends Actor
{
 
    public void act() 
    {
        this.touchingTheEdge();   
        this.turn(3);
        this.setLocation(this.getX() + 2, this.getY());
 
         
    }
    public void touchingTheEdge()
    {
        if(this.isAtEdge())
        {
            this.getWorld().removeObject(this);
        }
 
    }    
}
b. ) what do you mean with Btw, you can remove every instance of "this.". ???? Thanks again!!
Nosson1459 Nosson1459

2017/3/22

#
To answer both questions, your class can be this:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Ants here.
 * 
 * @author (your name)
 * @version (a version number or a date)
 */
public class Ants extends Actor
{

    public void act()
    {
        touchingTheEdge();
        turn(3);
        if (getWorld() != null) setLocation(getX() + 2, getY());
    }

    public void touchingTheEdge()
    {
        if(isAtEdge())
        {
            getWorld().removeObject(this);
        }
    }
}
wueschn wueschn

2017/3/22

#
@Nosson1459 Thanks a lot!!
You need to login to post a reply.