Solved NullPointerException on an event? Help!

Discussion in 'Plugin Development' started by frogman6102, Apr 27, 2015.

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

    frogman6102

    Code:
    @EventHandler
        public void playerDrink(PlayerItemConsumeEvent e){
            Player p = e.getPlayer();
           
           
            if(e.getItem().getItemMeta().getDisplayName().equals(ChatColor.RED + "Frogwolf Blood")){
           
                p.sendMessage(prefix + ChatColor.GRAY + ChatColor.BOLD + "You've drinkin' the blood of a FrogWolf! Oh no!");
       
            p.addPotionEffect(new PotionEffect(PotionEffectType.CONFUSION, 210, 1));
           
               
            }else{
                return;
            }
        }
    In game it does succesfully add the potion effect and send me the message. Although, when I drink milk I get this error:


    [20:42:23 ERROR]: Could not pass event PlayerItemConsumeEvent to FrogWolf v1.0
    org.bukkit.event.EventException
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:294) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
    at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav
    a:62) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
    at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.j
    ava:501) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
    at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
    ava:486) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
    at net.minecraft.server.v1_7_R3.EntityHuman.p(EntityHuman.java:274) [cra
    ftbukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
    at net.minecraft.server.v1_7_R3.EntityPlayer.p(EntityPlayer.java:885) [c
    raftbukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
    at net.minecraft.server.v1_7_R3.EntityHuman.h(EntityHuman.java:131) [cra
    ftbukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
    at net.minecraft.server.v1_7_R3.EntityPlayer.i(EntityPlayer.java:245) [c
    raftbukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
    at net.minecraft.server.v1_7_R3.PlayerConnection.a(PlayerConnection.java
    :343) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
    at net.minecraft.server.v1_7_R3.PacketPlayInFlying.a(SourceFile:137) [cr
    aftbukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
    at net.minecraft.server.v1_7_R3.PacketPlayInFlying.handle(SourceFile:8)
    [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
    at net.minecraft.server.v1_7_R3.NetworkManager.a(NetworkManager.java:157
    ) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
    at net.minecraft.server.v1_7_R3.ServerConnection.c(SourceFile:134) [craf
    tbukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
    at net.minecraft.server.v1_7_R3.MinecraftServer.v(MinecraftServer.java:6
    67) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
    at net.minecraft.server.v1_7_R3.DedicatedServer.v(DedicatedServer.java:2
    60) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
    at net.minecraft.server.v1_7_R3.MinecraftServer.u(MinecraftServer.java:5
    58) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
    at net.minecraft.server.v1_7_R3.MinecraftServer.run(MinecraftServer.java
    :469) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
    at net.minecraft.server.v1_7_R3.ThreadServerApplication.run(SourceFile:6
    28) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
    Caused by: java.lang.NullPointerException
    at com.frogsters.frogwolf.Events.playerDrink(Events.java:20) ~[?:?]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.7.0
    _75]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
    java:57) ~[?:1.7.0_75]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
    sorImpl.java:43) ~[?:1.7.0_75]
    at java.lang.reflect.Method.invoke(Method.java:606) ~[?:1.7.0_75]
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:292) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
    ... 17 more

    Line 20 is the if statement. Why is this occuring?? I've had this happen on multiple plugins?
     
  2. Offline

    Msrules123

    Because you aren't checking if the item has item meta before trying to access something that isn't there.
     
  3. Offline

    frogman6102

    @Msrules123 omg, I feel so stupid now. Brain fart. Sorry.
     
Thread Status:
Not open for further replies.

Share This Page