Solved Click outside of GUI

Discussion in 'Spigot Plugin Development' started by InfernoSkillz, Dec 1, 2018.

  1. So I have seen many threads of people asking the same thing and I have tried it out, yet, I doesn't fix my error.
    Basically, my problem is that it whenever players click outside of the inventory it sends me a giant error to console. For those saying that I should learn java, I am reading quite a few java books.

    Error:
    869fd9d065354b144b5315c076eb2851.png

    Code:
    Code (Text):
                if ((item.equals(Material.AIR) || !(item.hasItemMeta() || !(item.getItemMeta().hasDisplayName() || !(e.getInventory() == null) )))) {
                    return;
     
  2. The item variable is most likely null. I assume that it's assinged to InventoryClickEvent#getCurrentItem().
    Before using it (item.equals(...)) add a null check. In addition, ItemStack can't be compared with a material enum, the item.equals(Material.AIR) should be showing you an error on your IDE.
     
  3. To fix the error, add a check : if (item != null).
     
  4. Check if item is not null and use ItemStack#getType == Material.YOUR_MATERIAL
     
  5. SteelPhoenix

    Moderator

    if you click outside of the inventory event.getSlot() returns 999
     
  6. Check event.getClickedInventory() for null.
     
    • Agree Agree x 2
  7. As others pointed, add a non-null check so the code becomes:

    Code (Java):
      if (item != null)
        if ((item.equals(Material.AIR) || !(item.hasItemMeta() || !(item.getItemMeta().hasDisplayName() || !(e.getInventory() == null) )))) {
        // your code    
        }
    To further improve the coding standards, replace equals with == for enumerations. The equals is only necessary for instances of Objects like String or any class, wheres the rule of thumb for == is for primitive types, and since enumerations can only exist once you can just stick to ==:

    Code (Java):
      if (item != null)
        if ((item == Material.AIR || !(item.hasItemMeta() || !(item.getItemMeta().hasDisplayName() || !(e.getInventory() == null) )))) {
        // your code  
        }
     
    • Like Like x 1