Solved Getting a weird NPE

Discussion in 'Spigot Plugin Development' started by SpiroMarshes, Jun 23, 2016.

  1. Getting an odd NPE error. Trying to cancel crop breaking. Here is my method:

    Code (Text):
    @EventHandler
        public void onEntityInteract(PlayerInteractEvent event)
        {
            if (event.getClickedBlock().getType().equals(Material.SOIL) && event.getAction().equals(Action.PHYSICAL))
            {
                event.setCancelled(true);
            }else{
                event.setCancelled(false);
            }      
        }
    Here is the error: http://prntscr.com/bjsl3z
     
  2. What is on line 39?
     
  3. event.getClickedBlock() is null if the action isn't block-based. You're checking that first, which is throwing NPEs.
     
  4. So I should change my if statement to
    Code (Text):
    if (event.getAction().equals(Action.PHYSICAL) && event.getClickedBlock().getType().equals(Material.SOIL)  )
    correct?
     
  5. Fixed it, thanks.