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

2017/5/13

NullPointerException error

beastonion beastonion

2017/5/13

#
So here my code...
public void act() 
    {
        count=count++;
        List<M4> tank = getObjectsInRange(600, M4.class);
        M4 m4 = (M4)getOneIntersectingObject(M4.class);
        for(M4 tanks:tank)
        {
            tankX=tanks.getX();
            tankY=tanks.getY();
        } 
        move(-10);
        if(count<5)
        {
            turnTowards(tankX, tankY);
            setRotation(getRotation()+180);
            count=10;
        }
        if(isTouching(M4.class))
        {
            m4.damage(10);
        }
        if(getWorld()!=null &&isAtEdge()|| m4!=null)
        {
             die();
        }
        
        
    }    
    private void die()
    {
         getWorld().removeObject(this);
    }
This is the bullet that's hitting the tank and doing damage. It says the error occurs at "m4.damage(10)", when the tank took enough damage and was removed. I'm assuming it was trying to use that method but it's not there cause it was removed? I have no idea how are to fix this, I tried to call the removing of the bullet in the tank class and made it the last thing that happens, but it was not fixed :(
danpost danpost

2017/5/13

#
The for loop, lines 6 through 10, is equivalent to getting the x and y from the last tank in the list:
Actor actor = tank.get(tank.size()-1);
tankX = actor.getX();
tankY = actor.getY();
Line 15 is equivalent to:
turn(180);
and line 18 is equivalent to
if (m4 != null)
The 'getWorld() != null' condition on lines 22 is not needed as nothing prior to it is removing the actor from the world. Seems a bit strange to have the bullet turn away from the actor and move backwards. It would be easy to invert the image saved in the file. It would be much easier to have the tank detect and both remove the bullet and give itself the damage. Then, all the bullet would need to do is seek the tank, move and remove self when hitting an edge. The act method above would simply be:
public void act()
{
    count++;
    if (count < 5)
    {
        if (!getObjectsInRange(600, M4.class).isEmpty())
        {
            Actor m4 = (Actor)getObjectsInRange(600, M4.class).get(0);
            tankX = m4.getX();
            tankY = m4.getY();
        }
        turnTowards(tankX, tankY);
        turn(180);
        count = 10;
    }
    move(-10);
    if (isAtEdge()) die;
}
Then, in the M4 act method, you would have:
if (isTouching(Bullet.class))
{
    removeTouching(Bullet.class);
    damage(10);
}
Then check health at end of act for tank removal.
beastonion beastonion

2017/5/14

#
I'll try it out! Thank you very much for your response!
beastonion beastonion

2017/5/14

#
I've tried it, it worked fine at first, but when the tank starts to hit the base, I get "Actor not in world" error on the "isTouching(Bullet.class)". So I moved the code around and see if would change anything, not it just fly over the tank without getting rid of itself or apply the damage.
danpost danpost

2017/5/14

#
beastonion wrote...
I've tried it, it worked fine at first, but when the tank starts to hit the base, I get "Actor not in world" error on the "isTouching(Bullet.class)". So I moved the code around and see if would change anything, not it just fly over the tank without getting rid of itself or apply the damage.
My suggestion still stands. You are dealing with another issue there. Post the M4 class codes.
You need to login to post a reply.