Solved NullPointerException

Discussion in 'Plugin Development' started by boynedmaster, May 20, 2014.

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

    boynedmaster

    Here's my code. I get a NullPointerException at the selected line.
    Code:
    @EventHandler
    public void onEquip(PlayerItemHeldEvent e){
    if(disabled) return;
     
    if(e.getPlayer().getItemInHand().getItemMeta().getDisplayName() == "Rod of Sanic"){ //THIS IS WHERE I GET THE ERROR
    float newSpeed = e.getPlayer().getWalkSpeed() + 0.25F;
     
    if(newSpeed > 1)
    newSpeed = 1;
     
    pastSpeed.put(e.getPlayer(), e.getPlayer().getWalkSpeed());
     
    e.getPlayer().setWalkSpeed(newSpeed);
    }else{
    if(pastSpeed.containsKey(e.getPlayer())){
    e.getPlayer().setWalkSpeed(pastSpeed.get(e.getPlayer()));
    }else{
    pastSpeed.put(e.getPlayer(), e.getPlayer().getWalkSpeed());
    }
    }
    }
    
    whats happening
     
  2. Offline

    Bobit

    Which line?
    We can tell from your stack trace if you give it to us.
     
  3. Offline

    Gater12

    boynedmaster
    Your problems I can see:
    • Not checking if the item's ItemMeta is not null
    • Comparing String objects where it should be the value
    Solutions:
    • Add a null check to see if the item has an ItemMeta before accessing it.
    • Compare String values with equals() or equalsIgnoreCase()
     
  4. Offline

    xTigerRebornx

    Gater12 The ItemStack can also be null, he'll need a check for that too.
     
    Gater12 likes this.
  5. Offline

    boynedmaster

  6. Offline

    Bobit

    Oh...
    "//THIS IS WHERE I GET THE ERROR"
    [flintnsteel][tnt]
     
    Gater12 likes this.
Thread Status:
Not open for further replies.

Share This Page