0

I am getting a NPE (null-pointer exception) when placing any other item than the ones I have defined, or when drinking from a milk-bucket. I have tried adding isCanceled() == false to my EventHandler, but it breaks my RIGHT_CLICK_AIR action. Sorry, it sounds kind of complicated, any more info can be provided, thanks in advance!

@EventHandler(priority = EventPriority.HIGHEST)
public void onPlayerInteract(PlayerInteractEvent event) {
    Player p = event.getPlayer();
    Location location = p.getLocation();
    ItemStack item = event.getItem();
if(event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK && event.hasItem() != false || event.isCancelled() == true) {
caycehouse
  • 46
  • 1
  • 1
  • 6
  • Try breaking up your if statements to find logic bugs. Also, you can ignore cancelled events by using `@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)` – Jared Apr 30 '13 at 02:46
  • Can you show us your stack trace? And could you show us more of the code? – Kezz May 06 '13 at 07:29
  • Your code is fundamentally wrong: it will only execute if the event is cancelled, in which case you wouldn't be receiving the event in the first place (you don't have `ignoredCancelled = true`). Please post the full contents of `onPlayerInteract`. – Xyene May 31 '13 at 00:17

1 Answers1

0

I'd think it's because of this line:

ItemStack item = event.getItem();

When the player is not holding an item, it returns null. Try a null check to see if there is an item in their hand.

if (event.getItem() != null)){
   //do stuff
}
else{
   //don't do it
}