Solved NullPointerException

Discussion in 'Plugin Development' started by user_91277742, Nov 20, 2017.

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

    user_91277742

    Hello!,In the Event class I have an event to add a hat to your head if you have one of this and you die . The problem is when I die, I have a NulPointException here . In the first "if".

    Code:
        public void playerDeath(PlayerDeathEvent e) {
            Player p = e.getEntity();
            for(ItemStack item : contents(p.getInventory().getArmorContents(),p.getInventory().getContents())) {
      -->       if(item != null && item.equals(main.hatList("neko")) || item.equals(main.hatList("human")) ||
                        item.equals(main.hatList("elf)) || item.equals(main.hatList("hydra"))){
                    main.hatMap.put(p.getUniqueId(), item);
                    for(ItemStack i : e.getDrops()) {
                        if(i.equals(item)) {
                            i.setType(Material.AIR);
                        }
                    }
                }
            }
    
    If I have in this "if" my 4 hats , I have the null point exepction, but if i have just one like this..

    Code:
        public void playerDeath(PlayerDeathEvent e) {
            Player p = e.getEntity();
            for(ItemStack item : contents(p.getInventory().getArmorContents(),p.getInventory().getContents())) {
                if(item != null && item.equals(main.hatList("neko"))){
                    main.hatMap.put(p.getUniqueId(), item);
                    for(ItemStack i : e.getDrops()) {
                        if(i.equals(item)) {
                            i.setType(Material.AIR);
                        }
                    }
                }
            }
    
    The event and the plugin work with no problems.
     
  2. Offline

    timtower Administrator Administrator Moderator

    @AhrigumiWhat is the value of main?
    What does main.hatList("elf") return, and hydra? And the other ones?
     
  3. Offline

    Zombie_Striker

    @Ahrigumi
    The || means that if anything before this is true or anything afterwards is true, do the thing. That means the item can be null and it will still see if the item is from the human/elf/hydra list. Encapsulate correctly.
     
  4. Offline

    user_91277742

    the value of main is the mainclass and elf/neko/etc returns the hats

    So use || there is incorrect? I mean... If the item isnt null and "have the neko's hat" OR "have the elf's hat" ,etce,tc?
    Im cheking there if the item is not null and if you have a x hat

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
  5. Offline

    timtower Administrator Administrator Moderator

    @Ahrigumi The || is correct, but you need to but ( and ) around all the hats checks to group them as 1.
     
  6. Offline

    Zombie_Striker

    @Ahrigumi
    You need to encapsulate. Add brackets around the statements for the hats. (e.g. if item!=null && ( item.equals .... || item.equals ....) )
     
  7. Offline

    user_91277742

    Ah!, so... like @Zombie_Striker *he || means that if anything before this is true or anything afterwards is true, do the thing.* , So I need to group all statements into parenthesis.... Right?

    I will test it!

    P.D: Works! , Thank you so much @timtower & @Zombie_Striker
     
Thread Status:
Not open for further replies.

Share This Page