Debug this code?

Discussion in 'Plugin Development' started by triarry, Feb 7, 2013.

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

    triarry

    Code:
        public void blacklistItems(Player p) {
            if (p.hasPermission("pvprestore.blacklist.drop") && plugin.getConfig().getBoolean("blacklist.enabled") == true) {
                for (Integer itemList : plugin.getConfig().getIntegerList("blacklist.items")) {
                    p.getInventory().remove(itemList);
                    if (p.getInventory().getHelmet().getTypeId() == itemList) {
                        p.getInventory().setHelmet(null);
                    }
                }
            }
        }
    When I take out the

    Code:
                    if (p.getInventory().getHelmet().getTypeId() == itemList) {
                        p.getInventory().setHelmet(null);
                    }
    portion, it works just fine. Thoughts?

    EDIT: This is run when the player first joins the server, for debugging purposes currently. Could that be why?
     
  2. Offline

    raGan.

    Why would you check helmet in each cycle ? You do setHelmet(null) and then try to getHelmet().getTypeId().
     
    triarry likes this.
  3. Offline

    triarry

    What if I made it:

    Code:
    if (p.getInventory().getHelmet().getTypeId() == itemList && p.getInventory().getHelmet() != null)
     
  4. Offline

    raGan.

    or you could make it set and check it only once

    nevermind, do null check first
     
    triarry likes this.
  5. Offline

    triarry

    See, the iterator is checking all the values of itemList, which is a changing value in this situation. It needs to be checked for all values of itemList. The code I proposed also does not work.

    Even when I do the null check like I did above, it still fails...Definitely a strange error.

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

    raGan.

    You keep saying that is fails, but what kind of fail is it ? NPE ?
     
    triarry likes this.
  7. Offline

    triarry

    Yeah, it's an NPE whenever the code is called. Only while the helmet check code is in there, though. If I remove that, it compiles and works like normal (without checking and trying to remove a players helmet.)
     
  8. Offline

    raGan.

    if this throws NPE
    Code:
    if(p.getInventory().getHelmet() != null && p.getInventory().getHelmet().getTypeId() == itemList)
    then do
    Code:
    if(p.getInventory().getHelmet() != null)
        if(p.getInventory().getHelmet().getTypeId() == itemList)
    you could maybe even store player's inventory as local variable before running loop, to not do p.getInventory() all the time
     
    triarry likes this.
  9. Offline

    triarry

    Works! Awesome! Thanks! I forgot this little trick from when I was learning C++ last year, you're a life saver!
     
Thread Status:
Not open for further replies.

Share This Page