Solved InventoryClick NPE

Discussion in 'Plugin Development' started by ARCT3CK, Jan 27, 2015.

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

    ARCT3CK

    Hi, i have a headache with this code, can you help me?
    Code:
        @EventHandler
        public void onInventoryClick(InventoryClickEvent event) {
            Player player = (Player) event.getWhoClicked();
            if (event.getInventory().getTitle().equals("Tus opciones")) {
                if (event.getCurrentItem().getType() == Material.WATCH) {
                    if (!visibilidadOcultos.contains(player.getName())) {
                        ocultarJugadores(player);
                        player.chat("/opciones");
                    } else {
                        mostrarJugadores(player);
                        player.chat("/opciones");
    
                    }
                }
                event.setCancelled(true);
            }
        }
    Code works in question, but when i click an slot outside that custom inventory, i get a NPE :(
    Thank you
     
  2. Offline

    Konato_K

    getCurrentItem is null
     
    ARCT3CK likes this.
  3. Offline

    WinX64

    The event is called even if you click outside of the inventory, and when clicking outside of the inventory, the item you clicked doesn't even exist. Check if the clicked item exists before doing anything else with it.
     
    ARCT3CK likes this.
  4. Offline

    ARCT3CK

    How would i check if it exists? the
    if (event.getCurrentItem().getType().equals(Material.WATCH)) {
    won't work?
     
  5. Offline

    WinX64

    Simply check if the item is null before continuing
     
    ARCT3CK likes this.
  6. Offline

    CantBeCharged

    if(event.getCurrentItem() !=null)
     
    ARCT3CK likes this.
  7. Offline

    ARCT3CK

    Thank you!
    Worked, thank you very much!

    - Solved -
     
Thread Status:
Not open for further replies.

Share This Page