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

2013/10/31

NullPointerException problem

tim_31 tim_31

2013/10/31

#
I keep running into a NullPointerException problem could anyone please help me solve this issue. This problem occurs whenever I get close to a chest or get hit by an enemy (I think both are caused by the same problem in either Player.class or Dungeon.class, most likely Dungeon) My project
Entity1037 Entity1037

2013/10/31

#
THe problem likely lies in not checking if something your looking for exists. So for example something like this:
1
2
Actor player getOneIntersectingObject(player.class);
getWorld().removeObject(player);
will cause a null pointer exception if the player is not found, and the command is performed (it tries to remove something that does not exist). What appears to be the problem in your scenario is that your detecting the player, but not checking if the player exists. So something like this:
1
2
3
4
5
List players = getObjects(Player.class)
if (!players.isEmpty()){
    Actor player = (Actor) players.get(0);
    //Code that uses player
}
I can guarentee that the exception comes from using something that doesn't exist. So it seems you're using the player's coordinates when the player isn't checked to see if it exists. You can check if an actor exists by using this:
1
2
3
if ([actor name here]!=null){
    //use actor's coordinates however you want here
}
tim_31 tim_31

2013/10/31

#
Thanks for your quick reply, I will test this immediately.
Lee1121 Lee1121

2013/10/31

#
Entity1037 wrote...
THe problem likely lies in not checking if something your looking for exists. So for example something like this:
1
2
Actor player getOneIntersectingObject(player.class);
getWorld().removeObject(player);
will cause a null pointer exception if the player is not found, and the command is performed (it tries to remove something that does not exist). What appears to be the problem in your scenario is that your detecting the player, but not checking if the player exists. So something like this:
1
2
3
4
5
List players = getObjects(Player.class)
if (!players.isEmpty()){
    Actor player = (Actor) players.get(0);
    //Code that uses player
}
I can guarentee that the exception comes from using something that doesn't exist. So it seems you're using the player's coordinates when the player isn't checked to see if it exists. You can check if an actor exists by using this:
1
2
3
if ([actor name here]!=null){
    //use actor's coordinates however you want here
}
Hello Entity, I'm sorry, but it doesn't seem to work (I'm Tims partner by the way) This is what I've used:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public void checkPlayer()
    {
        List players = getWorld().getObjects(Player.class);
        if (!players.isEmpty()){ 
            Player playerObject = (Player) players.get(0);
            playerPoints = player.giveStat("playerPoints");
            int range = 64;
            List <Player> inRange = getObjectsInRange(range, Player.class);
            int size = inRange.size();
             
            if (size > 0)
            {
                checkMouse();
            }
        }
        else
        {
            System.out.println("Error, Dungeon can't find player");
        }
    }
Have I misunderstood something? Thanks for your time, Lee
Lee1121 Lee1121

2013/11/1

#
After a long time of trying different things over and over again, and getting help from someone from school, the problem has been fixed!
Entity1037 Entity1037

2013/11/1

#
You're not checking if the actor you got from the players list exists, and are calling givestat(String). Make sure you don't call something that isn't checked if it exists or not. Also, you might have to call givestat like this:
1
((Player)player).giveStat("playerPoints");
I'm not exactly sure what makes it required to use (()), however it seems like you need to do this sometimes when calling a method that is specific to a certain class only. Hope this helps.
tim_31 tim_31

2013/11/2

#
The problem has been solved, thanks for your time.
Entity1037 Entity1037

2013/11/2

#
Your welcome.
You need to login to post a reply.