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

2014/12/18

getting Null pointer

berdie berdie

2014/12/18

#
1
2
3
java.lang.NullPointerException
    at Boat.Exit(Boat.java:100)
    at Boat.act(Boat.java:27)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
  public void Exit(Exit out) {
      World world;
      world = getWorld();
  if (!world.getObjects(Boat.class).isEmpty())
  {
      if (intersects(out)){
          scoreUp();
          world.removeObject(this);
      }
      else if(isTouching(Exit.class)) {
          world.removeObject(this);
      }
  }
}
I'm getting a null pointer error with this function i don't know what it is going wrong:S?
Super_Hippo Super_Hippo

2014/12/18

#
Are you removing the actor from the world in the act method before this method is called?
berdie berdie

2014/12/18

#
Yes with
1
2
3
4
5
6
7
8
9
public void Colission() {
    Actor haven = getOneIntersectingObject(Haven.class);
    Actor boat = getOneIntersectingObject(Boat.class);
    if (haven != null || boat != null) {
        World world;
        world = getWorld();
        world.removeObject(this);
    }
}
But in this Exit method i wanted to check if the object is still in the world
Super_Hippo Super_Hippo

2014/12/18

#
You could start the method with "if (getWorld() == null) return;". This means, the method won't execute any further if the boat isn't in the world anymore.
danpost danpost

2014/12/18

#
The problem is with lines 3 and 4 of the Exit method. In order for 'getObjects' to execute properly on line 4, 'world' must not be assigned a 'null' value on line 3, which might be the case if the boat was removed from the world. So, the condition on line 4 will never be 'false'. It will either be 'true' or throw a NullPointerException. The Exit method should not be called unless your boat was still in the world to begin with. At any rate, the code should only be interested in whether the boat the method is executing for is in the world. In other words, one boat, in this case, should not care if others are in the world, only that it is. Also, the 'else' block of the 'if-else' on lines 6 through 12 is not needed. You are not wanting to remove the boat when intersecting ANY exit object, only when interesting the correct exit object which is what is checked for in the 'if' part of the code.
berdie berdie

2014/12/18

#
I would also like to remove the Boat when it reaches the Exit it shouldn't reach, because in the if I have the ScoreUp () method which increases the score
berdie berdie

2014/12/18

#
Problem solved!
You need to login to post a reply.