getInventory() Help

Discussion in 'Plugin Development' started by inventorman101, Jun 12, 2013.

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

    inventorman101

    I have been using the getInventory() method to check if it has an item with a certain display name and then removes one of those items on a onRightClick() event. It has a NullPointerException and I am not sure why here is my code

    Code:java
    1.  
    2. public class EventListener extends JavaPlugin implements Listener
    3. {
    4.  
    5. @EventHandler(priority = EventPriority.NORMAL) public void onPlayerUse(PlayerInteractEvent event)
    6. {
    7. Player p = event.getPlayer();
    8. {
    9. if (p.getItemInHand() != null) {
    10. if ((event.getAction() == Action.RIGHT_CLICK_BLOCK || event.getAction() == Action.RIGHT_CLICK_AIR) && itemCheck(p))
    11. {
    12. for(ItemStack i : p.getInventory().getContents())
    13. {
    14.  
    15. if(i.hasItemMeta())
    16. {
    17. ItemMeta a = i.getItemMeta();
    18. a.getDisplayName();
    19. if(a.getDisplayName()==("Cougar-Magnem Ammo"))
    20. {
    21. p.sendMessage("Fire");
    22. }
    23. }
    24.  
    25. }
    26. p.getInventory();
    27. } else if (event.getAction() == Action.RIGHT_CLICK_BLOCK || event.getAction() == Action.RIGHT_CLICK_AIR)
    28. {
    29. //nothing
    30. }
    31. }
    32. }
    33. }
    34.  
    35. public boolean itemCheck(Player player)
    36. {
    37. ItemStack i = player.getItemInHand();
    38. if(i == null)return false; // Fail the test if there is no item in hand
    39. if(!i.hasItemMeta())return false; // Fail the test if item does not have metadata
    40. ItemMeta im = i.getItemMeta();
    41. if(!im.hasDisplayName())return false; // Fail the test if the item doesn't have a display name
    42. String dn = im.getDisplayName();
    43. return dn.indexOf("Cougar-Magnem Ammo") == -1 && dn.indexOf("Cougar-Magnem") > -1;
    44. }
    45. }
    46.  



    Here is my StackTrace
    Code:
    Caused by: java.lang.NullPointerException
            at com.brendannoble.goldeneye.EventListener.onPlayerUse(EventListener.ja
    va:27)
            at sun.reflect.GeneratedMethodAccessor11.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)
            ... 16 more
    >
    
     
  2. Offline

    chasechocolate

    Maybe check if a.hasDisplayName() first.
     
  3. Offline

    inventorman101

    Ok, thanks for the tip :)

    That didn't seem to work here is my updated code (it still has the same stacktrace)

    Code:java
    1.  
    2. public class EventListener extends JavaPlugin implements Listener
    3. {
    4.  
    5. @EventHandler(priority = EventPriority.NORMAL) public void onPlayerUse(PlayerInteractEvent event)
    6. {
    7. Player p = event.getPlayer();
    8. {
    9. if (p.getItemInHand() != null) {
    10. if ((event.getAction() == Action.RIGHT_CLICK_BLOCK || event.getAction() == Action.RIGHT_CLICK_AIR) && itemCheck(p))
    11. {
    12. for(ItemStack i : p.getInventory().getContents())
    13. {
    14.  
    15. if(i.hasItemMeta())
    16. {
    17. ItemMeta a = i.getItemMeta();
    18. a.hasDisplayName();
    19. a.getDisplayName();
    20. if(a.getDisplayName()==("Cougar-Magnem Ammo"))
    21. {
    22. p.sendMessage("Fire");
    23. }
    24. }
    25.  
    26. }
    27. p.getInventory();
    28. } else if (event.getAction() == Action.RIGHT_CLICK_BLOCK || event.getAction() == Action.RIGHT_CLICK_AIR)
    29. {
    30. //nothing
    31. }
    32. }
    33. }
    34. }
    35.  
    36. public boolean itemCheck(Player player)
    37. {
    38. ItemStack i = player.getItemInHand();
    39. if(i == null)return false; // Fail the test if there is no item in hand
    40. if(!i.hasItemMeta())return false; // Fail the test if item does not have metadata
    41. ItemMeta im = i.getItemMeta();
    42. if(!im.hasDisplayName())return false; // Fail the test if the item doesn't have a display name
    43. String dn = im.getDisplayName();
    44. return dn.indexOf("Cougar-Magnem Ammo") == -1 && dn.indexOf("Cougar-Magnem") > -1;
    45. }
    46. }
    47.  


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 1, 2016
  4. Replace
    Code:java
    1. } else if (event.getAction() == Action.RIGHT_CLICK_BLOCK || event.getAction() == Action.RIGHT_CLICK_AIR)

    with
    Code:java
    1. } else if (event.getAction().equals(Action.RIGHT_CLICK_BLOCK) || event.getAction.equals(Action.RIGHT_CLICK_AIR))

    and see if that works :D
     
  5. Offline

    inventorman101

    That isn't the problem the problem is at if(i.hasItemMeta();){}
     
  6. Offline

    Shzylo

    that changed the ';' and ')' to ;) remove the ';' after hasItemMeta()
     
  7. Offline

    the_merciless

    You need a null check on each inventory item

    for (ItemStack i : p.getInventory().getContents()){
    if (i !=null ){
    //carry on
    }
    }getInventory().getContents())
     
  8. Offline

    inventorman101

    Ok thanks I will try that
     
Thread Status:
Not open for further replies.

Share This Page