Solved Getting A Error If It Is A Normal Stick

Discussion in 'Plugin Development' started by satarskrin, Oct 21, 2016.

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

    satarskrin

    So this is my code it works butt i am getting a error in the console when i use a stick that dosent have a custom name. How do I get rid of the error?

    Code:
        public void onplayerclick(PlayerInteractEvent event) {
            if (event.getAction().equals(Action.RIGHT_CLICK_AIR)) {
                Player player = event.getPlayer();
                ItemStack what = player.getItemInHand();
                if(what.getType() == Material.STICK){
                    if(what.getItemMeta().getDisplayName().equals(ChatColor.RED + "" + ChatColor.BOLD + "TheStick")){
                        player.sendMessage("This Is The Stick");
                        event.setCancelled(true);
                    }      
                }
              
        }
    
        }
     
  2. Offline

    Zombie_Striker

    When comparing enums, use ==.

    The itemstack can be null. If the player is not holding an item, the item will be null and this line will throw an NPE. Add a null check before getting the type.
    Main problem:
    1. The meta can be null. Make sure the item has itemmeta before you get the item meta.
    2. The displayname can be null. Make sure the item has a displayname before getting the displayname.
     
  3. Offline

    satarskrin

  4. Offline

    Zombie_Striker

  5. Offline

    satarskrin

Thread Status:
Not open for further replies.

Share This Page