Issues comparing item display name

Discussion in 'Spigot Plugin Development' started by bman7842, Jul 9, 2015.

  1. Back again with another issue on my puzzle plugin. I'm trying to determine whether or not the users selected items match the problem is something is returning null and I'm not for sure why it is. Either way, the if statement on line 106 in class Events can't determine it because of a nullpointer error. I don't see why it would be having this issue, I added null checks to the variables before it checked yet it still has the issue.

    If you have any suggestions on how I can improve my code or solutions to the issue I would love to hear them!

    GitHub link: https://github.com/bman7842/eclipsepuzzle

    Output:
    Code (Text):
    [10:44:27 INFO]: We made it boys
    [10:44:27 ERROR]: Could not pass event InventoryClickEvent to EclipsePuzzle v0.1

    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:310) ~[spigot.jar:git-Spigot-b3c0272-7723b90]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav
    a:62) ~[spigot.jar:git-Spigot-b3c0272-7723b90]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.j
    ava:502) [spigot.jar:git-Spigot-b3c0272-7723b90]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
    ava:487) [spigot.jar:git-Spigot-b3c0272-7723b90]
            at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java
    :1603) [spigot.jar:git-Spigot-b3c0272-7723b90]
            at net.minecraft.server.v1_8_R3.PacketPlayInWindowClick.a(SourceFile:31)
    [spigot.jar:git-Spigot-b3c0272-7723b90]
            at net.minecraft.server.v1_8_R3.PacketPlayInWindowClick.a(SourceFile:9)
    [spigot.jar:git-Spigot-b3c0272-7723b90]
            at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:1
    3) [spigot.jar:git-Spigot-b3c0272-7723b90]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [
    ?:1.8.0_45]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_45]
            at net.minecraft.server.v1_8_R3.SystemUtils.a(SystemUtils.java:19) [spig
    ot.jar:git-Spigot-b3c0272-7723b90]
            at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:7
    18) [spigot.jar:git-Spigot-b3c0272-7723b90]
            at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:3
    67) [spigot.jar:git-Spigot-b3c0272-7723b90]
            at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:6
    57) [spigot.jar:git-Spigot-b3c0272-7723b90]
            at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java
    :560) [spigot.jar:git-Spigot-b3c0272-7723b90]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_45]
    Caused by: java.lang.NullPointerException
            at me.bman7842.eclipsepuzzle.main.Events.inventoryInteract(Events.java:1
    06) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0
    _45]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0
    _45]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1
    .8.0_45]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_45]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:306) ~[spigot.jar:git-Spigot-b3c0272-7723b90]
            ... 15 more
     
  2. Check if the display name contains your string
     
  3. Check if it item has a display name before you check if it equals another string.
     
  4. You should't do correctItem.getItemMeta().getDisplayName().equals(...

    There are multiple places that can result in a null pointer, e.g. getItemMeta() can return null, getDisplayName() can return null.
     
  5. Do you suggest I instead save it in a variable and check it there? Then reference to that variable for comparing?
     
  6. I'd just check first if it has item meta and a name, like this:
    if(correctItem.hasItemMeta() && correctItem.getItemMeta().hasDisplayName())
     
  7. I added this and apparently one of these is returning false. I'm assuming ItemMeta but I'll check. (GitHub has been updated for reference)
     
  8. It has no itemmeta, why might this be?
     
  9. If hasItemMeta() returns false, the item can't have a custom name.
     
  10. Oohhhh I see the problem now. It's not custom it's the default name. Is there anyway for me to see the default name? Like getType and then use the material?
     
  11. Yeah, just item1.getType() == item2.getType()

    edit: after making sure that both item1 and item2 are not null of course
     
  12. Works, thanks so much for the help!