Solved NPE on ItemInHand DisplayName check.

Discussion in 'Plugin Development' started by kayc01, Jan 24, 2016.

Thread Status:
Not open for further replies.
  1. Hey quick error, hopefully a quick solution.

    I am checking when right clicking an item that it has a certain ItemMeta DisplayName.

    However it gives me an error that the check is null when i right click with a watch that does not have the DisplayName i am checking for.
    Is there a better way to check it?

    Right clicking the item with the correct DisplayName works perfectly.

    Code:
    Giving item to player:

    Code:
    Player p = e.getPlayer();
           
    
             ItemStack clock = new ItemStack(Material.WATCH, 1);
             ItemMeta itemMeta = clock.getItemMeta();
             itemMeta.setDisplayName(ChatColor.RED + (ChatColor.BOLD + "Badge Showcase"));
             itemMeta.setLore(Arrays.asList(ChatColor.GREEN + "View your badges!"));
             clock.setItemMeta(itemMeta);
            if (!p.getInventory().contains(clock)) {
             p.getInventory().addItem(clock);
    On right click:


    Code:
      @EventHandler
            public void onPlayerInteract(PlayerInteractEvent e) {
                //Action a = e.getAction();
                //ItemStack is = e.getItem();
                Player p = e.getPlayer();
               
               
                if(e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction().equals(Action.RIGHT_CLICK_AIR) || e.getAction().equals(Action.RIGHT_CLICK_BLOCK)) {
                    if (p.getPlayer().getItemInHand().getType().equals(Material.WATCH)) {
                        if (p.getPlayer().getInventory().getItemInHand().getItemMeta().getDisplayName().equals(ChatColor.RED + (ChatColor.BOLD + "Badge Showcase"))) {
                            Bukkit.dispatchCommand(p, "gym see " +p.getName());
             }
                    }
                else {
                    //do nothing
                }
          }
            }
    So the error is caused by:
    Code:
    if (p.getPlayer().getInventory().getItemInHand().getItemMeta().getDisplayName().equals(ChatColor.RED + (ChatColor.BOLD + "Badge Showcase"))) {
    Any help is appreciated ! :D
     
  2. Offline

    87pen

    Check if the item in hand is null, check if the item has meta, check if the items name is not null. Then check if it has the name you want.
    @kayc01
     
  3. Thanks, this worked:


    Code:
    if(e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction().equals(Action.RIGHT_CLICK_AIR) || e.getAction().equals(Action.RIGHT_CLICK_BLOCK)) {
                    if (p.getPlayer().getItemInHand().getType().equals(Material.WATCH)) {
                      if (p.getPlayer().getInventory().getItemInHand().getItemMeta() != null) {
                          if (p.getPlayer().getInventory().getItemInHand().getItemMeta().getDisplayName() != null) {
                        if (p.getPlayer().getInventory().getItemInHand().getItemMeta().getDisplayName().equals(ChatColor.RED + (ChatColor.BOLD + "Badge Showcase"))) {
                            Bukkit.dispatchCommand(p, "gym see " +p.getName());
                        }
                          }
             }
                    }
                else {
                    //do nothing
                }
          }
    (added):


    Code:
     if (p.getPlayer().getInventory().getItemInHand().getItemMeta() != null) {
                          if (p.getPlayer().getInventory().getItemInHand().getItemMeta().getDisplayName() != null) {
    Solved.
     
  4. Offline

    87pen

    @kayc01 Instead of using != null you can also use p.getItemInHand().hasItemMeta()
     
  5. Alright thanks, will use in future :)
     
Thread Status:
Not open for further replies.

Share This Page