Inventory click event error?

Discussion in 'Plugin Development' started by Go Hard, Sep 10, 2013.

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

    Go Hard

    Hello,

    I am making a plugin and when my player clicks inside of a custom inventory it works fine but if they are still in the inventory and click outside of the inventory it gives me this error.

    Code:
    2013-09-11 01:58:06 [SEVERE] Could not pass event InventoryClickEvent to Test v1.0
    org.bukkit.event.EventException
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:427)
    at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62)
    at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:477)
    at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:462)
    at net.minecraft.server.v1_6_R2.PlayerConnection.a(PlayerConnection.java:1372)
    at net.minecraft.server.v1_6_R2.Packet102WindowClick.handle(SourceFile:31)
    at net.minecraft.server.v1_6_R2.NetworkManager.b(NetworkManager.java:296)
    at net.minecraft.server.v1_6_R2.PlayerConnection.e(PlayerConnection.java:116)
    at net.minecraft.server.v1_6_R2.ServerConnection.b(SourceFile:37)
    at net.minecraft.server.v1_6_R2.DedicatedServerConnection.b(SourceFile:30)
    at net.minecraft.server.v1_6_R2.MinecraftServer.t(MinecraftServer.java:590)
    at net.minecraft.server.v1_6_R2.DedicatedServer.t(DedicatedServer.java:226)
    at net.minecraft.server.v1_6_R2.MinecraftServer.s(MinecraftServer.java:486)
    at net.minecraft.server.v1_6_R2.MinecraftServer.run(MinecraftServer.java:419)
    at net.minecraft.server.v1_6_R2.ThreadServerApplication.run(SourceFile:582)
    Caused by: java.lang.NullPointerException
    at me.Predator2012.test.ItemListener.onInventoryClick(ItemListener.java:61)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:425)
    ... 14 more
    
    my inventory click event:

    Code:
    @EventHandler
        public void onInventoryClick (InventoryClickEvent e) {
    Player p = (Player) e.getWhoClicked();
     
            if (e.getInventory().getTitle().equals(ti)) {
          if(e.getCurrentItem().getType().equals(null)) {
    e.setCancelled(true);
     
          }
    }
          if(e.getCurrentItem().getType().equals(Material.APPLE)) {
          e.setCancelled(true);
          p.sendMessage("Hello");
          p.closeInventory();
          ti.clear();
      }
    }
    }
    
    Any way i can fix this error?
     
  2. Offline

    uyuyuy99

    You're one curley bracket off:
    Code:
    @EventHandler
        public void onInventoryClick (InventoryClickEvent e) {
    Player p = (Player) e.getWhoClicked();
     
            if (e.getInventory().getTitle().equals(ti)) {
          if(e.getCurrentItem().getType().equals(null)) {
    e.setCancelled(true);
     
          } else {
                if(e.getCurrentItem().getType().equals(Material.APPLE)) {
                e.setCancelled(true);
                p.sendMessage("Hello");
                p.closeInventory();
                ti.clear();
          }
    }
      }
    }
    }
    You didn't put the code that checks if the item is an apple inside the inventory title check. Also, I added an "else" statement so the apple check doesn't run if the clicked item is null.
     
  3. Offline

    Go Hard

    uyuyuy99
    I tried the }else{ and didnt work.
    Its not when i click the apple its when i click outside of the inventory only when im in my custom inventory
     
  4. you're getting a null pointer exception(NPE) because you click outside of an inventory, but you tell your code that it IS an inventory and start checking it's name. Do a null check before you start checking the name of the inventory. if there are no inventory, there are no name
     
  5. Offline

    Go Hard

    mollekake
    I see what you're saying. How would i put that in my code?
     
  6. Offline

    MinecraftMart

    Got the same problem and i didnt find any way to fix it.
     
  7. Offline

    fireblast709

  8. Offline

    MinecraftMart

    fireblast709
    The top of the event
    Code:java
    1. @EventHandler
    2. public void onInventoryClick(InventoryClickEvent e) {
    3. if (!e.getInventory().getName().equalsIgnoreCase(inv.getName())) return;
    4. if (e.getCurrentItem().getItemMeta() == null) return;
    5. if (!e.getInventory().getName().equalsIgnoreCase(inv.getName())) return;
    6. if (e.getCurrentItem().getItemMeta() == null) return;
    7.  
    8. if(e.getCurrentItem() == null){
    9. return;
    10. }
    11. else{


    Still gives an error
     
  9. Offline

    Necrodoom

    MinecraftMart make a new thread describing your problem, with full code and stacktrace.
     
  10. Offline

    fireblast709

    MinecraftMart first check getCurrentItem() for null, then check the ItemMeta and then check the name
     
  11. Offline

    Necrodoom

    fireblast709 He made a new thread in which his problem was solved.
     
  12. Offline

    Batman500

    I use:
    Code:java
    1. if(itemstack != null && itemstack.getType() != Material.AIR) {
    2. //CODE
    3. }
     
Thread Status:
Not open for further replies.

Share This Page