Solved Inventory NullPointerException

Discussion in 'Plugin Development' started by SeniorCluckers, Mar 5, 2017.

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

    SeniorCluckers

    I check if the current item is null but it gives me a null exception still, I have others of the same and they work fine. Just when I disable the event below it stops giving the error.

    PHP:
        @EventHandler
        
    public void onBlocksShopInventory(InventoryClickEvent event) {
           
            
    Player player = (Playerevent.getWhoClicked();
            
    ItemStack clicked event.getCurrentItem();
            
    Inventory inventory event.getClickedInventory();
            
    String name clicked.getItemMeta().getDisplayName().toString();
           
            
    ItemStack playerIron = new ItemStack(Material.IRON_INGOT);
            
    ItemStack priceIron = new ItemStack(Material.IRON_INGOT4);
            
    ItemStack wool = new ItemStack(Material.WOOL16);

            if (
    event.getCurrentItem() == null) {
                return;
            } else {
                if (
    inventory.getName().equals(ItemStacks.blocksshopInventory.getName())) {
                    if (
    clicked.getType() == Material.WOOL && clicked.getAmount() == 16) {
                        if (
    player.getInventory().containsAtLeast(playerIron4)) {
                            
    player.getInventory().removeItem(priceIron);
                            
    player.getInventory().addItem(wool);
                            
    player.sendMessage(purchase ChatColor.stripColor(name));
                        }
                    }
                }
            }
        }
    Code:
    Caused by: java.lang.NullPointerException
            at com.seniorcluckers.bedwars.utils.inv.Shop.onBlocksShopInventory(Shop.java:184) ~[?:?]
            at sun.reflect.GeneratedMethodAccessor4.invoke(Unknown Source) ~[?:?]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_121]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_121]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot.jar:git-Spigot-21fe707-e1ebe52]
     
  2. Offline

    timtower Administrator Administrator Moderator

    @SeniorCluckers You check event.getCurrentItem and clicked. Are those the same?
     
  3. Offline

    SeniorCluckers

    @timtower
    Not sure exactly what your asking for but when I use clicked instead of event I get dead code.
     
  4. Offline

    timtower Administrator Administrator Moderator

  5. Offline

    Zombie_Striker

    Something here is null.
    1. clicked can be null. Null check this
    2. The item may not have item meta. Check if the Item#hasItemMeta
    3. The item may not have a displayname. Check if ItemMeta#hasDisplayName
     
  6. Offline

    SeniorCluckers

    @timtower

    Its the full method.


    @Zombie_Striker
    Code:
            if (clicked == null) {
                if(!clicked.hasItemMeta()) {
                    if (clicked.getItemMeta().getDisplayName() == null) {
                      
                        return;
                    }
                }
            }
    I removed string name and just used clicked directly and got no pointer exception but it would be easier to use a string.
     
    Last edited: Mar 5, 2017
  7. Offline

    Zombie_Striker

    @SeniorCluckers
    That won't work. You are trying to check if all three cases are true instead of if any of those cases are. Instead of three ifs, merge them into one and use the or operator ( || )
     
  8. Offline

    SeniorCluckers

    @Zombie_Striker

    Really lost.. lol

    Code:
            if (clicked == null || clicked.hasItemMeta() == false || clicked.getItemMeta().hasDisplayName() == false) {
                return;
            }
     
  9. Offline

    Zombie_Striker

    @SeniorCluckers
    Well, all you need to do now is add this bit of code before you create the "name" string.
     
    Theztc likes this.
  10. Offline

    SeniorCluckers

Thread Status:
Not open for further replies.

Share This Page