Solved How to solve this?

Discussion in 'Plugin Development' started by mine-care, Apr 27, 2014.

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

    mine-care

    Well im trying to get the name of a potion chosen to give player the potion effect. soo there is a inventory and when you chose a potion you get the effect. So i used this string:
    Code:java
    1. String s = ChatColor.stripColor(e.getCurrentItem()
    2. .getItemMeta().getDisplayName().replace("Potion of", "")
    3. .replace(" ", "_").toUpperCase());
    4.  


    and im getting this error:
    Code:
    [11:51:36 ERROR]: Could not pass event InventoryClickEvent to SimpleGui v1.0
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:320) ~[craftbukkit-1.7.2-R0.3.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav
    a:62) ~[craftbukkit-1.7.2-R0.3.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.j
    ava:486) [craftbukkit-1.7.2-R0.3.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
    ava:471) [craftbukkit-1.7.2-R0.3.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
            at net.minecraft.server.v1_7_R1.PlayerConnection.a(PlayerConnection.java
    :1361) [craftbukkit-1.7.2-R0.3.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
            at net.minecraft.server.v1_7_R1.PacketPlayInWindowClick.a(SourceFile:32)
    [craftbukkit-1.7.2-R0.3.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
            at net.minecraft.server.v1_7_R1.PacketPlayInWindowClick.handle(SourceFil
    e:10) [craftbukkit-1.7.2-R0.3.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
            at net.minecraft.server.v1_7_R1.NetworkManager.a(NetworkManager.java:146
    ) [craftbukkit-1.7.2-R0.3.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
            at net.minecraft.server.v1_7_R1.ServerConnection.c(SourceFile:134) [craf
    tbukkit-1.7.2-R0.3.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
            at net.minecraft.server.v1_7_R1.MinecraftServer.u(MinecraftServer.java:6
    55) [craftbukkit-1.7.2-R0.3.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
            at net.minecraft.server.v1_7_R1.DedicatedServer.u(DedicatedServer.java:2
    50) [craftbukkit-1.7.2-R0.3.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
            at net.minecraft.server.v1_7_R1.MinecraftServer.t(MinecraftServer.java:5
    45) [craftbukkit-1.7.2-R0.3.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
            at net.minecraft.server.v1_7_R1.MinecraftServer.run(MinecraftServer.java
    :457) [craftbukkit-1.7.2-R0.3.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
            at net.minecraft.server.v1_7_R1.ThreadServerApplication.run(SourceFile:6
    17) [craftbukkit-1.7.2-R0.3.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    Caused by: java.lang.NullPointerException
            at me.fillpant.SimpleGui.Main.invcle(Main.java:109) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.7.0
    _51]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.7.0
    _51]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1
    .7.0_51]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.7.0_51]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:318) ~[craftbukkit-1.7.2-R0.3.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
            ... 13 more
    >
    
    Note that line 109 is the string given above^
    Any ideas?
     
  2. Offline

    Deleted user

  3. Offline

    mine-care

  4. Offline

    garbagemule

    While the above link may be useful, it doesn't address an important issue with this post...

    If a single statement in your code is choc full of method invocations (and chained method invocations, at that), and it needs to be line-wrapped twice, you're asking for problems. Your biggest problem right now is that you have absolutely no clue where that error comes from, because you have an unreasonable amount of potential sources in one line of code. Compact code is not the same as efficient code, and we have pretty big monitors and high resolutions nowadays...

    In your single statement, you have six chained method invocations:
    1. getCurrentItem()
    2. getItemMeta()
    3. getDisplayName()
    4. replace()
    5. replace()
    6. toUpperCase()
    Making the (somewhat naïve) assumption that you don't know for sure if any of these methods can return null, and whether or not the variable e can be null (I'm assuming it's an Event, so it probably isn't), you thus have six places that the NullPointerException could be thrown from.

    So, which ones of those methods can return null? And which one is it most likely to be? The easiest way to figure out is to split the line up into logical parts - it's taking up three lines, so just make it into three (or more) statements.
    Code:java
    1. ItemStack item = e.getCurrentItem();
    2. ItemMeta meta = item.getItemMeta();
    3. String name = meta.getDisplayName();
    4. name = name.replace("Potion of", "").replace(" ", "_").toUpperCase();
    5. String s = ChatColor.stripColor(name);

    Splitting the code up like that will give you a much better indication of where your issue is, because when the error is thrown, it will most likely be thrown from a line with a single method invocation (replace() and toUpperCase() will never return null, so if that line throws the exception, it's name that's a null pointer). If you can merge a couple of lines, that's fine, but only do it if you're absolutely sure you won't ever call a method on a reference you don't know for sure won't return null. Read the documentation (and the source code) of the methods you're calling, so you have a better understanding of the API you're using.

    Happy hacking.
     
  5. Offline

    mine-care


    :eek: thanks for the tip, im using eclipse and i use ctrl+sft+f and auto wraps it.
    Thanks a lot!
     
Thread Status:
Not open for further replies.

Share This Page