Solved NullPointException

Discussion in 'Plugin Development' started by SirQuantum, Jun 7, 2016.

Thread Status:
Not open for further replies.
  1. I'm calling on the inventory click event to restrict icons of a vault that players can click on, it keeps throwing this error however and I can't see any direct fixes. Help is appreciated

    Code:
    @EventHandler
        public void InvClick(InventoryClickEvent e) {
            if(e.getCurrentItem().getType()==null || e.getCurrentItem().getType()==Material.AIR) return;
            if(e.getCurrentItem() == null || e.getCurrentItem().getType() == Material.AIR) return;
            if(e.getInventory().getName().equalsIgnoreCase("vault")) {
                if(e.getCurrentItem().getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.RED+"Slot locked!"))  {
                    e.setCancelled(true);
                }
            }
        }
    And this is my error

    Code:
    [17:59:13 ERROR]: Could not pass event InventoryClickEvent to EnderVaults v0.0.1
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:305) ~[server.jar:git-Spigot-870264a-0a645a2]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[server.jar:git-Spigot-870264a-0a645a2]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [server.jar:git-Spigot-870264a-0a645a2]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [server.jar:git-Spigot-870264a-0a645a2]
            at net.minecraft.server.v1_8_R2.PlayerConnection.a(PlayerConnection.java:1620) [server.jar:git-Spigot-870264a-0a645a2]
            at net.minecraft.server.v1_8_R2.PacketPlayInWindowClick.a(SourceFile:31) [server.jar:git-Spigot-870264a-0a645a2]
            at net.minecraft.server.v1_8_R2.PacketPlayInWindowClick.a(SourceFile:9) [server.jar:git-Spigot-870264a-0a645a2]
            at net.minecraft.server.v1_8_R2.PlayerConnectionUtils$1.run(SourceFile:13) [server.jar:git-Spigot-870264a-0a645a2]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_31]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_31]
            at net.minecraft.server.v1_8_R2.SystemUtils.a(SourceFile:60) [server.jar:git-Spigot-870264a-0a645a2]
            at net.minecraft.server.v1_8_R2.MinecraftServer.A(MinecraftServer.java:712) [server.jar:git-Spigot-870264a-0a645a2]
            at net.minecraft.server.v1_8_R2.DedicatedServer.A(DedicatedServer.java:368) [server.jar:git-Spigot-870264a-0a645a2]
            at net.minecraft.server.v1_8_R2.MinecraftServer.z(MinecraftServer.java:651) [server.jar:git-Spigot-870264a-0a645a2]
            at net.minecraft.server.v1_8_R2.MinecraftServer.run(MinecraftServer.java:554) [server.jar:git-Spigot-870264a-0a645a2]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_31]
    Caused by: java.lang.NullPointerException
            at com.enjin.heavenpvpcraft.plugin.EventHandling.InvClick(EventHandling.java:41) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_31]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_31]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_31]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_31]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:301) ~[server.jar:git-Spigot-870264a-0a645a2]
            ... 15 more
     
  2. Offline

    Xerox262

    @SirQuantum You need to check if the item is null before checking if the item type is null, and you'll get a null pointer on getDisplayName(); if the item doesn't have one, you can also get one on getItemMeta();.
     
  3. @Xerox262
    So I just need to check them in order?

    Code:
    if(e.getCurrentItem()==null) return;
    if(e.getCurrentItem().getType()==null) return;
    if(e.getCurrentItem().getItemMeta()==null) return;
    if(e.getCurrentItem().getItemMeta().getDisplayName()==null) return;
    if(e.getInventory().getName().equalsIgnoreCase(ChatColor.RED+"vault") {
        if(e.getCurrentItem().getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.RED+"slot locked!")) {
           e.setCancelled(true);
       }
    }
     
  4. Offline

    Xerox262

    @SirQuantum Should be fine I don't think the Item type can be null though.
     
  5. @Xerox262 Eitherway it works fine without any errors, thanks for your help!
     
    Xerox262 likes this.
Thread Status:
Not open for further replies.

Share This Page