Check how much of an item the player has in inventory

Discussion in 'Plugin Development' started by Nic2555, Jul 12, 2015.

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

    Nic2555

    Hi i'm trying to find how many of a certain item the players has in their inventory. I got this code but it gives me a NullPointerException on the line 299 (in this case line 9 ):
    Code:
    if(Material.matchMaterial((String) listFind.get(index).get("type")) != null)
                            {
                                ItemStack it = new ItemStack(Material.matchMaterial((String) listFind.get(index).get("type")));
                                boolean isValide = false;
                                int ind = 0;
                                ItemStack[] inv = e.getPlayer().getInventory().getContents();
                                e.getPlayer().sendMessage("test");
                                for(int i = 0; i < e.getPlayer().getInventory().getSize(); i++) {
                                    e.getPlayer().sendMessage(e.getPlayer().getInventory().getItem(i).getType().name()); // line 299
                                    if(e.getPlayer().getInventory().getItem(i).getType().equals(it.getType()))
                                    {
                                        isValide = true;
                                        ind = i;
                                    }
                                }
    i still have no idea why,
    Thanks !

    -Nic
     
    Last edited: Jul 12, 2015
  2. Offline

    ChipDev

    What is line 299?
     
  3. Offline

    Nic2555

    i wrote it in comment, it's the one that promp the item name to the player chat.,
     
  4. Offline

    ChipDev

    Oops. sorry. Maybe getInventory doesn't have i.
     
  5. Offline

    Zombie_Striker

    .getInventory().getItem(i).getType().name()'

    Is the Inventory null? is the item null? is the type null? is the name null?

    You did not even check for any of those things. Why did you not check if any of these are null. You should always get into the habit of adding null checks.
     
  6. Offline

    Burn38

    As Zombie_Striker said just insert null checks, I think that empty slots returns null ItemStacks and that's why you get NullPointerException.
     
  7. Offline

    Nic2555

    yea that was indeed the problem. I feel a bit bad right now, But thanks anyways it fixed my problem :)
    have a nice day !

    - Nic
     
Thread Status:
Not open for further replies.

Share This Page