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

2013/1/16

Help!

JellyBlobsofDoom JellyBlobsofDoom

2013/1/16

#
This error comes up when a bullet hits a bomb... Java.lang.NullPointerException at Bullet.worldEdge(Bullet.java:33) at Bullet.act(Bullet.java:21) at greenfoot.core.Simulation.actActor(Simulation.java:565) at greenfoot.core.Simulation.runOneLoop(Simulation.java:523) at greenfoot.core.Simulation.runContent(Simulation.java:213) at greenfoot.core.Simulation.run(Simulation.java:203) The code for my bullet: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Bulleyt here. * * @author (your name) * @version (a version number or a date) */ public class Bullet extends Animal { /** * Act - do whatever the Bulleyt wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { move(); canSeeC(); worldEdge(); } public void move() { move(4); } public void worldEdge() { if (!(getWorld().getObjects(Bullet.class).isEmpty())) { if ( getX() < 20 || getX() > getWorld().getWidth() - 20) { getWorld().removeObject(this); } else if(getY() < 20 || getY() > getWorld().getHeight() - 20) { getWorld().removeObject(this); } } } public void canSeeC() { Bomb bomb = (Bomb) getOneObjectAtOffset(0, 0, Bomb.class); if (bomb !=null) { getWorld().removeObject(bomb); getWorld().removeObject(this); } } }
danpost danpost

2013/1/16

#
Your act method calls 'worldEdge', which needs the actor to be in the world, after calling 'canSeeC', which has the capability of removing the actor from the world (hence the error occurs when the actor sees a bomb). Simple fix: only call the 'worldEdge' method if the actor is still in the world:
if(getWorld() != null) worldEdge();
JellyBlobsofDoom JellyBlobsofDoom

2013/1/16

#
Thank you ! :)
MatheMagician MatheMagician

2013/1/16

#
You are continuing to call methods when your actor has been removed. Just make the worldEdge() call occur if the bullet does not hit the bomb.
/**
 * Write a description of class Bulleyt here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Bullet extends Animal
{
    /**
     * Act - do whatever the Bulleyt wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        move();
        canSeeC();
    }    
    public void move()
    {
        move(4);
    }

    public void worldEdge()
    {
        if (!(getWorld().getObjects(Bullet.class).isEmpty()))
        {
            if ( getX() < 20 || getX() > getWorld().getWidth() - 20)
            {
                getWorld().removeObject(this);
            }
            else if(getY() < 20 || getY() > getWorld().getHeight() - 20)
            {
                getWorld().removeObject(this);
            }  
        }
    }

    public void canSeeC()
    {
        Bomb bomb = (Bomb) getOneObjectAtOffset(0, 0, Bomb.class);
        if (bomb !=null)
        {
            getWorld().removeObject(bomb);
            getWorld().removeObject(this);
        }
        else worldEdge();
    }

}
MatheMagician MatheMagician

2013/1/16

#
Oops! Danpost's solution is a little simpler than mine and I wrote mine before his was posted, so disregard the above post.
JellyBlobsofDoom JellyBlobsofDoom

2013/1/16

#
Thanks for the help though! :)
You need to login to post a reply.