ItemStack NPE?

Discussion in 'Plugin Development' started by x2nec, Jan 12, 2013.

Thread Status:
Not open for further replies.
  1. Code:
    Caused by: java.lang.NullPointerException
            at org.bukkit.inventory.ItemStack.<init>(ItemStack.java:64)
            at me.moxie.util.LevelUpEvent.reward(LevelUpEvent.java:141)
            at me.moxie.util.LevelUpEvent.levelUp(LevelUpEvent.java:35)
            at me.moxie.XPlisteners.SwordXP.SwordsXPProcess(SwordXP.java:63)
            at me.moxie.XPlisteners.SwordXP.EntityDeath(SwordXP.java:42)
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
            at java.lang.reflect.Method.invoke(Unknown Source)
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:425)
            ... 21 more
    That's the error I get, now the code it relates to is this:

    Code:
    for(int i : t.keySet()){
                        if((lvl % i)==0 || i == 1){
                            check=true;
                            String s = t.get(i);
                            String[] x = s.split(",");
                            String type = x[0];
                            String ID = x[1];
                            Integer amount = Integer.parseInt(x[2]);
                            if(type.equalsIgnoreCase("item")){
                                p.getInventory().addItem(new ItemStack(Material.getMaterial(ID), amount));
                                p.sendMessage(ChatColor.GOLD+"You received a reward: " + ChatColor.GREEN+amount+" " + Material.getMaterial(ID).toString().toLowerCase());
                            } else if(type.equalsIgnoreCase("xp")){
                                p.setTotalExperience(p.getTotalExperience()+amount);
                                p.sendMessage(ChatColor.GOLD+"You received a reward: " + ChatColor.GREEN+amount+" XP");
                            } else if(type.equalsIgnoreCase("money")){
                                if(this.m.EconomyEnabled==false)
                                    return;
                                Moxie.economy.depositPlayer(p.getName(), amount);
                                p.sendMessage(ChatColor.GOLD+"You received a reward: " + ChatColor.GREEN+amount+" " + Moxie.economy.currencyNamePlural());
                            }
                        }
                    }
    I have checked both ID and amount, and they produce the values 89 and 3 as expected, which should mean the itemstack added is 3 glowstone. Yet I get that error. Any help please?
     
  2. Offline

    Ne0nx3r0

    Specifically which line is the error related to?
     
  3. Ne0nx3r0

    p.getInventory().addItem(new ItemStack(Material.getMaterial(ID), amount));
    It says this line gives the NPE
     
  4. Offline

    Ne0nx3r0

    The way I usually troubleshoot these is by checking the variables individually and after they are grabbed from methods, so something like:
    Code:
    System.out.println(p);
    System.out.println(p.getInventory());
    System.out.println(ID);
    System.out.println(amount);
    System.out.println(Material.getMaterial(ID));
    

    Assuming the cause of the error is coming from that line, and not someplace higher/lower in the stacktrace.
     
  5. Ne0nx3r0

    Aha!

    Code:
    System.out.println(Material.getMaterial(ID));
    This is what returns null, but surely it should be fine as the ID is 89 - glowstone, it should be able to "get" that right?
     
  6. Offline

    Ne0nx3r0

    Try changing ID from a String to an int, something like:
    Code:
    int ID;
    try
    {
          ID = Integer.parseInt(x[1]);
    }
    catch (Exception e)
    {
        // Not a valid int
    }

    *edit*

    Notably, here's a code sample I've used in the past to first match a string (IE: "glowstone", then attempt to convert to an int and try 89):
    Code:
            Material m = null;
            try {
              m = Material.getMaterial(Integer.parseInt(args[0]));
            } catch (Exception e) {
              m = Material.matchMaterial(args[0]);
     
              if ((m == null) || (!m.isBlock())) {
                this.p.msg(player, ChatColor.RED + "'" + args[0] + "' is not a valid block!");
                this.p.msg(player, "(hint: diamond_block vs diamond)");
                return true;
              }
            }
     
  7. Ne0nx3r0
    Ah! Now you say it that way, it does seem like a possible reason! I'll try it out now!

    Ne0nx3r0

    Worked, thanks!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jul 8, 2016
Thread Status:
Not open for further replies.

Share This Page