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:

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

    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(
    :1603) [spigot.jar:git-Spigot-b3c0272-7723b90]
            at net.minecraft.server.v1_8_R3.PacketPlayInWindowClick.a(SourceFile:31)
            at net.minecraft.server.v1_8_R3.PacketPlayInWindowClick.a(SourceFile:9)
            at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$
    3) [spigot.jar:git-Spigot-b3c0272-7723b90]
            at java.util.concurrent.Executors$ Source) [
            at Source) [?:1.8.0_45]
            at net.minecraft.server.v1_8_R3.SystemUtils.a( [spig
            at net.minecraft.server.v1_8_R3.MinecraftServer.B(
    18) [spigot.jar:git-Spigot-b3c0272-7723b90]
            at net.minecraft.server.v1_8_R3.DedicatedServer.B(
    67) [spigot.jar:git-Spigot-b3c0272-7723b90]
            at net.minecraft.server.v1_8_R3.MinecraftServer.A(
    57) [spigot.jar:git-Spigot-b3c0272-7723b90]
    :560) [spigot.jar:git-Spigot-b3c0272-7723b90]
            at Source) [?:1.8.0_45]
    Caused by: java.lang.NullPointerException
            at me.bman7842.eclipsepuzzle.main.Events.inventoryInteract(
    06) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_45]
    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!