Solved NullPointerException

Discussion in 'Plugin Development' started by RoboticPlayer, Sep 4, 2015.

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

    RoboticPlayer

    I am working on a plugin that makes items unbreakable, and it works perfectly, except for one thing. I am getting a NullPointerException in my onEnable(), and I don't exactly know why.
    Here is my error: http://pastebin.com/0FGPm2J0
    Here is my onEnable:
    Code:
        @Override
        public void onEnable() {
            registerListeners();
    
            Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
                @Override
                public void run() {
                    for (Player p : Bukkit.getServer().getOnlinePlayers()) {
                        for (ItemStack a : p.getInventory().getArmorContents()) {
                            a.setDurability((short) (a.getDurability() - 2000));
                        }
    
                        for (ItemStack h : p.getInventory().getContents()) {
    /*LINE 37 HERE!*/      if (h.getType().equals(Material.WOOD_AXE) || h.getType().equals(Material.WOOD_HOE)
                                    || h.getType().equals(Material.WOOD_PICKAXE) || h.getType().equals(Material.WOOD_SWORD)
                                    || h.getType().equals(Material.WOOD_SPADE) || h.getType().equals(Material.STONE_AXE)
                                    || h.getType().equals(Material.STONE_HOE) || h.getType().equals(Material.STONE_PICKAXE)
                                    || h.getType().equals(Material.STONE_SWORD) || h.getType().equals(Material.STONE_SPADE)
                                    || h.getType().equals(Material.IRON_AXE) || h.getType().equals(Material.IRON_HOE)
                                    || h.getType().equals(Material.IRON_PICKAXE) || h.getType().equals(Material.IRON_SWORD)
                                    || h.getType().equals(Material.IRON_SPADE) || h.getType().equals(Material.GOLD_AXE)
                                    || h.getType().equals(Material.GOLD_HOE) || h.getType().equals(Material.GOLD_PICKAXE)
                                    || h.getType().equals(Material.GOLD_SWORD) || h.getType().equals(Material.GOLD_SPADE)
                                    || h.getType().equals(Material.DIAMOND_AXE) || h.getType().equals(Material.DIAMOND_HOE)
                                    || h.getType().equals(Material.DIAMOND_PICKAXE)
                                    || h.getType().equals(Material.DIAMOND_SWORD)
                                    || h.getType().equals(Material.DIAMOND_SPADE)) {
                                h.setDurability((short) (h.getDurability() - 2000));
                            }
                        }
    
                    }
    
                }
            }, 5L);
        }
     
  2. Offline

    DuaneTheDev_

    You can make life a lot more easier!

    Code:
    List<Material> Items = Arrays.asList(Material.WOOD_HOE, Material.WOOD_PICKAXE, Material.WOOD_SWORD, Material.WOOD_SPADE, Material.STONE_AXE,
                    Material.STONE_HOE, Material.STONE_PICKAXE, Material.STONE_SWORD, Material.STONE_SPADE, Material.STONE_AXE,
                    Material.IRON_HOE, Material.IRON_PICKAXE, Material.IRON_SWORD, Material.IRON_SPADE, Material.IRON_AXE,
                    Material.GOLD_HOE, Material.GOLD_PICKAXE, Material.GOLD_SWORD, Material.GOLD_SPADE, Material.GOLD_AXE,
                    Material.DIAMOND_HOE, Material.DIAMOND_PICKAXE, Material.DIAMOND_SWORD, Material.DIAMOND_SPADE, Material.DIAMOND_AXE,
                    Material.BOW);
               for(ItemStack Item : p.getInventory().getContents()) {
                   if(Items.contains(Item.getType())) {
                       if(Item.getDurability() != 0) {
                           Item.setDurability((short) 0);
                       }
                   }
               }
              
     
    Last edited: Sep 4, 2015
  3. Offline

    CARR0T

    @henderry2019
    First of all you should use == instead of .equals() (Just to get into good practise)

    You should check if h equals null first.

    Code:
    if(h != null) {
         Code in here.
    }
     
  4. Offline

    RoboticPlayer

    Thanks, I tried it out and it worked!

    @CARR0T I think that worked, I haven't had any issues!
     
  5. Offline

    Zombie_Striker

    @henderry2019
    If you are not receiving any more errors, mark as solved.
     
  6. Offline

    RoboticPlayer

Thread Status:
Not open for further replies.

Share This Page