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

2017/3/28

No Exception Message

rockon411 rockon411

2017/3/28

#
I am working on a code with a reduceHealth() method and it seems to work, but when I test it I get the error No Exception Message. I would really appreciate any help. This is in my actor class.
public void reduceHealth(int reduce)
    {
        health = health - reduce;
        if (health <= 0)
        {
            Island island = (Island) getWorld();
            island.remove(this);
        }
    }
This is in my test class.
public void testReduceHealth()
    {
        bob.reduceHealth(3);
        
        assertEquals(0, ant.getHealth());
        assertEquals(0, colony.getObjects(Minnow.class).size());
    }
danpost danpost

2017/3/28

#
What is the 'remove' method in the Island class?
davmac davmac

2017/3/28

#
The complete stack trace should identify the line on which the exception occurs, and the type of the exception, which should both greatly help you (and us) determine what the issue is. Please post the full stack trace and identify which line in the code you posted above is the one which causes the exception.
rockon411 rockon411

2017/3/28

#
The error says
java.lang.NullPointerException
    at Minnow.reduceHealth(Minnow.java:55)
    at MinnowTest.testReduceHealth(MinnowTest.java:58)
Those lines correspond to
island.remove(this);
and
bob.reduceHealth(3);
rockon411 rockon411

2017/3/28

#
Also the method isn't one that I created, it's given by greenfoot: remove(Actor actor) Remove an Actor from the world.
danpost danpost

2017/3/28

#
rockon411 wrote...
Also the method isn't one that I created, it's given by greenfoot: remove(Actor actor) Remove an Actor from the world.
There is not any 'remove' method in either the Actor class or in the World class. The closest thing in the Actor class is a 'removeTouching' method that will remove one intersecting object of the given parameter class type and the closest things in the World class are the 'removeObject' and 'removeObjects' methods. To have an actor remove itself from a world, you would use:
getWorld().removeObject(this);
// or in your case, you could use
island.removeObject(this);
rockon411 rockon411

2017/3/28

#
Thanks I seem to be doing something incorrectly, however, because I changed
island.remove(this);
to
island.removeObject(this);
and when I try to compile it, I receive the error "cannot find symbol - method removeObject(Minnow)"
You need to login to post a reply.