Solved Checking Armor throws NPE

Discussion in 'Plugin Development' started by Irantwomiles, Nov 22, 2016.

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

    Irantwomiles

    I'm not sure what I'm doing wrong here, but what ever I've tried just throws an error out. I'm trying to see if the player is wearing a certain piece of armor and if so give them certain potion effects and I keep getting this error. http://prntscr.com/dalb1m

    Here is my code that I'm checking .

    PHP:
        public void checkBard(Player player) {
            
    ItemStack helmet = new ItemStack(Material.GOLD_HELMET);
            
    ItemStack chest = new ItemStack(Material.GOLD_CHESTPLATE);
            
    ItemStack leg = new ItemStack(Material.GOLD_LEGGINGS);
            
    ItemStack boots = new ItemStack(Material.GOLD_BOOTS);
      
            if(!
    player.getInventory().getHelmet().getType().equals(null)) {
                if(
    player.getInventory().getHelmet().getType().equals(helmet)) {
                    
    player.sendMessage("helmet");
                }
            }
          
        }
    This is inside the onEnable()
    Code:
        new BukkitRunnable() {
                @SuppressWarnings("deprecation")
                public void run() {
                  
                    for(Player p : Bukkit.getOnlinePlayers()) {
                        setBoard(p);
                        checkBard(p);
                    }
                  
                }
            }
            .runTaskTimer((Plugin) this, 0L, 10L);
    
    Any ideas?
     
  2. Offline

    ShaneCraftDev

    Casting Plugin to, what ever the value of "this" might be is bad practice. If "this" is already an instance of Plugin, this cast is unnecessary. There are a lot of mistakes here. The error probably originates from your player not wearing a helmet. You're checking the Material of the helmet in a player's inventory, but the player might not be wearing one, thus resulting the PlayerInventory#getHelmet to return null. However there is another big problem with your code, you're comparing Material with ItemStack. You can compare the Material of the helmet directly to the gold helmet one, no need to create a new ItemStack instance

    I would advice to store the helmet in a variable, now you're accessing the Material of the worn helmet twice. You should merge the 2 if-statements and add a null check on the returned helmet.

    Code:java
    1.  
    2. ItemStack helmet = player.getInventory().getHelmet();
    3. if (helmet != null && helmet.getType() == Material.GOLD_HELMET) // run code..
    4.  


    Another note, do not use equals for enums, use ==.
     
    Irantwomiles likes this.
  3. Offline

    Irantwomiles

    Ok thanks for the tips, and for the casting of the plugin, that was eclipse. This is in my onEnable and it needed the field of my main class and I put 'this' but it was telling me to cast it to 'Plugin'.
    EDIT: Thanks for the help @ShaneCraftDev , that was my problem!
     
Thread Status:
Not open for further replies.

Share This Page