getNearbyEntities not getting players

Discussion in 'Plugin Development' started by nuclearmissile, Jan 4, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    nuclearmissile

    I have this code to get entities within 30 meters:

    Code:
    List<Entity> affected = player.getNearbyEntities(30, 30, 30);
                            for (Entity temp:affected){
                                if (temp instanceof LivingEntity){
                                    LivingEntity temp2 = (LivingEntity) temp;
                                    if(temp2.getHealth() > 6){
                                        temp2.setHealth(temp2.getHealth() - 6);
                                    }
                                    else temp2.setHealth(0);
                                }
                            }
    It works fine on animals, but for some reason it won't harm me in survival mode. When I right-click with the bomb it throws a NullPointerException at this line:

    Code:
    if(action.equals(Action.RIGHT_CLICK_BLOCK) && !player.getItemInHand().equals(null)){
    Does anybody know what my problem here is? All help is greatly appreciated!
     
  2. Offline

    Shayana

    We need the code before your if statement to help you, but I would say either action or player is null.
     
  3. Offline

    AGuyWhoSkis

    nuclearmissile Always check if objects are null before handling them! (If it is possible they could be null)
     
  4. Offline

    nuclearmissile

    Ok so here is the code before that if statement up to and including it:

    Code:
    public void interact(PlayerInteractEvent e){
            final Player player = e.getPlayer();
            Action action = e.getAction();
            final Location loc = GetBlockLookingAt(player).getLocation();
            loc.setX(loc.getX() + 0.5);
            loc.setZ(loc.getZ() + 0.5);
            loc.setY(loc.getY() + 0.5);
            if(action.equals(Action.RIGHT_CLICK_BLOCK) && !player.getItemInHand().equals(null)){
    I don't seem to be getting the NullPointerExceptions consistently, but it still never inflicts damage on the player. Any ideas?
     
  5. Offline

    Ironraptor3

    Hmm. Depends if you are trying to test it by damaging yourself, it won't work. Nearby entities ignores the player you are getting them around
     
  6. Offline

    Doamax

    Try putting the !player.getItemInHand().equals(null) before action.equals(Action.RIGHT_CLICK_BLOCK)
     
  7. Offline

    nuclearmissile

    Thank you very much Ironraptor, that's exactly what my problem was! I added some code to handle the player independently from the other nearby entities and everything was just fine. Thanks! And about the exceptions, it seems the null handling code I have now fixed it, I played around with it and it's not throwing any more exceptions. Thank you all for the suggestions and assistance!
     
  8. Offline

    bigteddy98

    I always use this method to get the nearby entities, this inludes everything what lives, including the player.

    Code:
        public List<LivingEntity> getEntities(Location loc, int distance) {
            List<LivingEntity> monsters = new ArrayList<LivingEntity>();
            for (Entity e : loc.getWorld().getEntities()) {
                if (e instanceof LivingEntity) {
                    if (e.getLocation().distance(loc) <= distance) {
                        monsters.add((LivingEntity) e);
                    }
                }
            }
            return monsters;
        }
    You might wanna use it.

    Btw, try to change
    Code:java
    1. if(action.equals(Action.RIGHT_CLICK_BLOCK) && !player.getItemInHand().equals(null)){

    to:
    Code:java
    1. if(action.equals(Action.RIGHT_CLICK_BLOCK) && player.getItemInHand() != null){

    That should work.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 6, 2016
  9. What the fuck? If you put the cause of the npe first or last doesn't matter, it's still the cause of npe!

    bigteddy98 has the correct solution. .equals(null) will either result in false (when the object is null) or a npe (when it is null). So don't use methods when checking for a null object.
     
Thread Status:
Not open for further replies.

Share This Page