Solved Why is this giving me a NullPointerExeception?

Discussion in 'Plugin Development' started by Zach_1919, Jun 4, 2013.

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

    Zach_1919

    So I have this code, before it was giving me NullPointerExceptions. I added a null check and a check to see if the item had ItemMeta before comparing the ItemMeta. Now, it is giving me an error where I checked if the item has ItemMeta? Help?

    Code:
    Code:
        public void onInteract(PlayerInteractEvent event)
        {
            Player player = event.getPlayer();
            Action action = event.getAction();
            if(player.getItemInHand() != null)
            {
                if(player.getItemInHand().hasItemMeta())
                {
                    if(player.getItemInHand().getItemMeta().getDisplayName().equals(ChatColor.GOLD + "XP " + ChatColor.AQUA + "Jar"))
                    {
                        if(action.equals(Action.LEFT_CLICK_AIR) || action.equals(Action.LEFT_CLICK_BLOCK))
                        {
                            if(!(Integer.parseInt(player.getItemInHand().getItemMeta().getLore().get(0)) <= 0))
                            {
    It's giving me an error on this:
    Code:
    if(player.getItemInHand().hasItemMeta())
    
     
  2. Offline

    Jamesthatguy

    Whats the error?
     
  3. You're actually not checking if the item in hand has meta.
    You're just checking if there's even an item in hand.
    Try adding getItemMeta() for the first if statement.
    If that throws a NullPointerException (which could only be
    the case with nothing in hand [be sure to test that!]), add it
    after the first if statement.

    And your issue should be solved!
     
  4. Offline

    ZeusAllMighty11

    Not all items have a display name! Or lore!
     
  5. Offline

    Zach_1919

    InteritumStudios But I thought that hasItemMeta() returned true or false and if it was false then there is no ItemMeta. Hmmm so I should do this instead:

    Code:
    if(player.getItemInHand().getItemMeta() != null)
    
    Or is it because some items have item meta, but not lore, so I should do the code above followed by this:

    Code:
    if(player.getItemInHand().getItemMeta().getLore() != null)
    
    Right?

    Jamesthatguy Sorry for double-posting but I will post the stack trace when I get back on my computer :)

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 1, 2016
  6. Offline

    Seadragon91

    If the player has no item in the hand, the itemstack from getItemInHand() is null. Add a null check:
    Code:
    if(player.getItemInHand() != null && player.getItemInHand().hasItemMeta())
     
  7. Offline

    Rocoty

    That's exactly what he's doing...
     
  8. Offline

    Zach_1919

    Rocoty Thanks for letting him know, that was right in my original code :p
     
  9. Offline

    Rocoty

    Zach_1919 Could you provide the stacktrace please?
     
  10. Offline

    Zach_1919

    Rocoty Here's my updated code:
    Code:
    package me.Zach_1919.xpjar;
     
    import java.util.ArrayList;
    import java.util.List;
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.Sound;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerBucketFillEvent;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.ShapedRecipe;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class Main extends JavaPlugin implements Listener
    {
        ShapedRecipe xpjar = new ShapedRecipe(setStartingData(new ItemStack(Material.GLASS_BOTTLE), ChatColor.GOLD + "XP " + ChatColor.AQUA + "Jar")).shape(" ! ","@#@"," $ ").setIngredient('!', Material.DIAMOND).setIngredient('@', Material.GOLD_INGOT).setIngredient('#', Material.BLAZE_POWDER).setIngredient('$', Material.GLASS_BOTTLE);
     
        public void onEnable()
        {
            getServer().getPluginManager().registerEvents(this, this);
            Bukkit.addRecipe(xpjar);
        }
        public void onDisable()
        {
            Bukkit.clearRecipes();
        }
        @EventHandler
        public void onInteract(PlayerInteractEvent event)
        {
            Player player = event.getPlayer();
            Action action = event.getAction();
            if(player.getItemInHand() != null)
            {
                ERROR: if(!(player.getItemInHand().getItemMeta().getLore().isEmpty()))
                {
                    if(player.getItemInHand().getItemMeta().getDisplayName().equals(ChatColor.GOLD + "XP " + ChatColor.AQUA + "Jar"))
                    {
                        if(action.equals(Action.LEFT_CLICK_AIR) || action.equals(Action.LEFT_CLICK_BLOCK))
                        {
                            if(!(Integer.parseInt(player.getItemInHand().getItemMeta().getLore().get(0)) <= 0))
                            {
                                player.playSound(player.getLocation(), Sound.ORB_PICKUP, 1, 1);
                                player.setLevel(player.getLevel()+1);
                                player.getItemInHand().setItemMeta(subLore(player.getItemInHand().getItemMeta()));
                            }
                            else
                            {
                                player.sendMessage(ChatColor.DARK_PURPLE + "[" + ChatColor.GOLD + "XP" + ChatColor.AQUA + "Jar" + ChatColor.DARK_PURPLE + "]" + ChatColor.RED + "This bottle does not have enough XP!");
                            }
                        }
                        if(action.equals(Action.RIGHT_CLICK_AIR) || action.equals(Action.RIGHT_CLICK_BLOCK))
                        {
                            if(!(player.getLevel() <= 0))
                            {
                                player.playSound(player.getLocation(), Sound.ORB_PICKUP, 1, 1);
                                player.setLevel(player.getLevel()-1);
                                player.getItemInHand().setItemMeta(addLore(player.getItemInHand().getItemMeta()));
                            }
                            else
                            {
                                player.sendMessage(ChatColor.DARK_PURPLE + "[" + ChatColor.GOLD + "XP" + ChatColor.AQUA + "Jar" + ChatColor.DARK_PURPLE + "]" + ChatColor.RED + "You do not have enough XP!");
                            }
                        }
                    }
                }
            }
        }
        public void onBucketFill(PlayerBucketFillEvent event)
        {
            ItemStack item = event.getItemStack();
            if(item.equals(setStartingData(new ItemStack(Material.GLASS_BOTTLE), ChatColor.GOLD + "XP " + ChatColor.AQUA + "Jar")))
            {
                event.setCancelled(true);
            }
        }
        public ItemStack setStartingData(ItemStack item, String name)
        {
            ItemMeta meta = item.getItemMeta();
            List<String> lore = new ArrayList<String>();
            lore.add("0");
            meta.setDisplayName(name);
            meta.setLore(lore);
            item.setItemMeta(meta);
            return item;
        }
        public ItemMeta addLore(ItemMeta meta)
        {
            List<String> lore = meta.getLore();
            String product = String.valueOf(Integer.parseInt(lore.get(0)) + 1);
            lore.set(0, product);
            meta.setLore(lore);
            return meta;
        }
        public ItemMeta subLore(ItemMeta meta)
        {
            List<String> lore = meta.getLore();
            String product = String.valueOf(Integer.parseInt(lore.get(0)) - 1);
            lore.set(0, product);
            meta.setLore(lore);
            return meta;
        }
    }
    
    I decided to change the getItemMeta() != null to !getLore().isEmpty() because I thought it might be giving me an error because of there being no lore, but it's still giving me an error on that line. Here's the error:
    [​IMG]
     
  11. Offline

    Rocoty

    what about checking hasLore()?
     
  12. Offline

    inventorman101

    I hate NullPointerException's they take FOREVER to find out the stupid problem and it is always simple!!!
     
  13. Offline

    Zach_1919

    Rocoty That exists? That might help...

    EDIT: nope
     
  14. Offline

    Technius

    inventorman101 They do not take long to fix. An average NPE only takes me about ten seconds to fix.

    Zach_1919
    ItemMeta could be null and the item's lore could be null.
    Code:
    private final xpJarName = ChatColor.GOLD + "XP " + ChatColor.AQUA + "Jar";
    public boolean isXPJar(ItemStack i)
    {
         if(i == null)return false; //Ignore if no item is in the hand
         if(!i.hasItemMeta())return false; //Ignore if the item has no metadata
         ItemMeta im = i.getItemMeta();
         if(im.hasLore())return false; //Ignore if the item has lore
         if(!im.hasDisplayName())return false; //Ignore if the item has no display name
         return xpJarName.equals(im.getDisplayName());
    }
    
     
  15. Offline

    Zach_1919

    Technius Well I just tried it on my server and it worked! Thanks!
     
  16. Offline

    inventorman101

    Technius sometimes that is the case but not always. You can't assume until you have seen the problem.
     
Thread Status:
Not open for further replies.

Share This Page