NullPointerException in EntityDamageEvent

Discussion in 'Plugin Development' started by LibRMX, Jul 17, 2020.

Thread Status:
Not open for further replies.
  1. Hey!
    I have a really weird error that i can't get fixed
    Code:
        @EventHandler
        public void onEntityDamage(EntityDamageEvent e) {
            if(e.getEntity() != null) {
                if(e.getEntity() instanceof Player) {
                    if(e.getEntity().getLastDamageCause().getCause() == DamageCause.FALL) {
                            e.setCancelled(true); 
                        }
                    }
                } 
            }

    the Null Pointer is on if(e.getEntity().getLastDamageCause().getCause() == DamageCause.FALL)

    if i connect to the server, and try to get fall damage, i get damage and the error shows up but if i'm trying it again, the error isn't showing up and it works.

    B if i reconnect it happens again the first time you take damage​
     
  2. Offline

    Strahan

    getLastDamageCause is annotated @Nullable, so you should check if it's null first rather than hang a method off of it.
     
  3. How?
    i already tried to do like if(e.getEntity().getLastDamageCause() != null)
     
  4. Offline

    timtower Administrator Administrator Moderator

    @LibRMX But did you do both null checks?
     
  5. For getLastDamageCause and getCause?

    Now i don't get any errors in the console, but on the first login, and the first fall, i still get damage but after the first one it works.

    But when i quit and join again it happens again

    Code:
        @EventHandler
        public void onEntityDamage(EntityDamageEvent e) {
            if(e.getEntity() != null) {
                if(e.getEntity() instanceof Player) {
                    if(e.getEntity().getLastDamageCause() != null) {
                        if(e.getEntity().getLastDamageCause().getCause() != null) {
                            if(e.getEntity().getLastDamageCause().getCause() == DamageCause.FALL) {
                                e.setCancelled(true);   
                            }
                        }
                    }
                }
            }   
        }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jul 18, 2020
  6. i really don't know why it does it
     
  7. Thread can be closed! It works now, thanks for your help
     
    Last edited: Jul 20, 2020
  8. Offline

    Strahan

    Glad you figured it out.

    Christ that is painful code to look at. You should consider negative check and return, it would get rid of that "large arrow" indentation effect, lol. Here is an example of your method, refactored:
    Code:
    @EventHandler
    public void onEntityDamage(EntityDamageEvent e) {
      if (e.getEntity() == null) return;
      if (!(e.getEntity() instanceof Player)) return;
      if (e.getEntity().getLastDamageCause() == null) return;
      if (e.getEntity().getLastDamageCause().getCause() == null) return;
      if (e.getEntity().getLastDamageCause().getCause() != DamageCause.FALL) return;
    
      e.setCancelled(true); 
    }
    Now isn't that much easier to read and understand? Granted, it's just my person opinion but I think this is a much better way to design things. Just FYI :)
     
Thread Status:
Not open for further replies.

Share This Page