Development Assistance NPE on custom Inventory Contains Method

Discussion in 'Plugin Help/Development/Requests' started by BetaNyan, Nov 27, 2014.

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

    BetaNyan

    So I made a shop plugin that is on a sign, and there is a selling feature. The selling feature works, but not with damaged items, and I want damaged items. So I made my own contains method that compares names and not items.

    This is the code

    Code:java
    1. public boolean contains(Player p, ItemStack it, int quanity) {
    2. if (p.getInventory().getContents().length != 0) {
    3. p.sendMessage("debug not 0");
    4. for (ItemStack i : p.getInventory().getContents()) {
    5. p.sendMessage("debug loop");
    6. if (i.toString().equals(it.toString()) && i.getAmount() >= quanity) {
    7. p.sendMessage("debug true");
    8. i.setDurability((short)-1);
    9. if (quanity != 1) {
    10. for (int a=0;a<quanity;a++) {
    11. p.getInventory().remove(it);
    12. p.updateInventory();
    13. }
    14. } else {
    15. p.getInventory().removeItem(i);
    16. p.updateInventory();
    17. }
    18. return true;
    19. } else {
    20. p.sendMessage("debug false");
    21. continue;
    22. }
    23. }
    24. }
    25. return false;
    26. }


    I get a NPE at "if (i.toString().equals)" line.

    Any reasons why?

    Thanks!
     
  2. BetaNyan
    The item is null (aka air). Check if the item is not null first:
    Code:
    if(i == null) {
        continue;
    }
     
Thread Status:
Not open for further replies.

Share This Page