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

2019/2/28

nullPointerExecption error for hit detection

1
2
Notted Notted

2019/2/28

#
Working on a Space Invader remake. A nullPointerExecption error is being risen whenever spaceWall is trying to check whenever a bullet has hit it.
private void wallTakesAHit()
    {
        boolean playersBulletHasHitWall = ((MyWorld)getWorld()).getBulletForPlayer().hasHitWall();
        if (playersBulletHasHitWall == true)
        {
            wallsHitPoints = wallsHitPoints - 1;
        }
    }
Is this too much?
Zestix Zestix

2019/2/28

#
Please show us the whole code
Notted Notted

2019/2/28

#
Ok. This good enough:
boolean playersBulletHasHitWall = isTouching(worldBullet.class);
boolean aliensBulletHasHitWall = isTouching(xenoBullet.class);
Now we will deal with this:
 private void wallCollapse()
    {
        MyWorld spaceInvWorld = (MyWorld) getWorld();
        if (wallsHitPoints <= 0)
        {
            spaceInvWorld.removeObject(this); 
        }
    }
It's casuing a "java.lang.IllegalStateException: Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed." I'm not sure how to solve this.
Zestix Zestix

2019/2/28

#
Like I said please post the whole code
Notted Notted

2019/2/28

#
Zestix wrote...
Like I said please post the whole code
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class spaceWall here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class spaceWall extends Actor
{
    /**
     * This is a wall. It takes hits for the player.
     * 
     */
    private int wallsHitPoints = 3; 
    public void act() 
    {
        wallCollapse();
        wallTakesAHit();
    } 
    private void wallCollapse()
    {
        MyWorld spaceInvWorld = (MyWorld) getWorld();
        if (wallsHitPoints <= 0)
        {
            spaceInvWorld.removeObject(this); 
        }
    }
    private void wallTakesAHit()
    {
        boolean playersBulletHasHitWall = isTouching(worldBullet.class);
        boolean aliensBulletHasHitWall = isTouching(xenoBullet.class);
        if (playersBulletHasHitWall == true || aliensBulletHasHitWall == true)
        {
            wallsHitPoints = wallsHitPoints - 1;
        }
    }
   
}
Zestix Zestix

2019/2/28

#
I don't really know why you'd need line 23 you can just remove that and replace line 26 with getWorld().removeObject(this);
Notted Notted

2019/2/28

#
Zestix wrote...
I don't really know why you'd need line 23 you can just remove that and replace line 26 with getWorld().removeObject(this);
Does not matter either way. Still causes "java.lang.IllegalStateException"
Zestix Zestix

2019/2/28

#
Change private void wallCollapse() to
private void wallCollapse()
    {
       
        if (wallsHitPoints <= 0)
        {
if (!getWorld().getObjects(spaceWall.class).isEmpty())
{
            getWorld().removeObject(this); 
}
        }
    }
Zestix Zestix

2019/2/28

#
Line 6 just checks if the actor is in the world
Notted Notted

2019/2/28

#
Hmm. boolean playersBulletHasHitWall = isTouching(worldBullet.class); seems to be causing the actual probelm.
Zestix Zestix

2019/2/28

#
Please show the worldBullet class code then
Notted Notted

2019/2/28

#
Zestix wrote...
Please show the worldBullet class code then
Ok.
;import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class worldBullet here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class worldBullet extends Actor
{
    /**
     * This is a bullet. It hits actors and flys.
     * 
     */
    public void act() 
    {
        flyUpwards(10);
    }    
    private int flyUpwards(int speed) 
    {
        setLocation(getX(), getY() - speed);
        return speed;
    } 
    public boolean hasHitWall()
    {
       boolean hitWall = isTouching(spaceWall.class);
       return hitWall;
    }
}
Zestix Zestix

2019/2/28

#
Notted wrote...
Zestix wrote...
Please show the worldBullet class code then
Ok.
;import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class worldBullet here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class worldBullet extends Actor
{
    /**
     * This is a bullet. It hits actors and flys.
     * 
     */
    public void act() 
    {
        flyUpwards(10);
    }    
    private int flyUpwards(int speed) 
    {
        setLocation(getX(), getY() - speed);
        return speed;
    } 
    public boolean hasHitWall()
    {
       boolean hitWall = isTouching(spaceWall.class);
       return hitWall;
    }
}
I don't really see a problem here and I'm not really sure if that solves anything but maybe try seperating the two conditions in WallTakesAHit() like this:
private void wallTakesAHit()
    {
        boolean playersBulletHasHitWall = isTouching(worldBullet.class);
        boolean aliensBulletHasHitWall = isTouching(xenoBullet.class);
        if (playersBulletHasHitWall == true)
        {
            wallsHitPoints = wallsHitPoints - 1;
        }
        if (aliensBulletHasHitWall == true))
{
    wallsHitPoints = wallsHitPoints- 1;
}
    }
Notted Notted

2019/2/28

#
What I want now is a reference to worldBullet. The code for the following is:
private void wallTakesAHit()
    {
        MyWorld spaceInvWorld = (MyWorld) getWorld();
        boolean playersBulletHasHitWall = isTouching(worldBullet.class);
        boolean aliensBulletHasHitWall = isTouching(xenoBullet.class);
        worldBullet playersBullet2 = spaceInvWorld.getBulletForPlayer();
        if (playersBulletHasHitWall == true)
        {
            wallsHitPoints = wallsHitPoints - 1;   
            spaceInvWorld.removeObject(playersBullet2); 
        }
    }
Getter methods:
private worldBullet playersBullet;
    public worldBullet getBulletForPlayer()
    {
        return playersBullet;
    }
Zestix Zestix

2019/2/28

#
I don't think you have to do it that way. Can't you just remove it buy using the command removeTouching(worldBullet.class); ?
There are more replies on the next page.
1
2