NullPointerException when not using gadget?

Discussion in 'Plugin Development' started by SuperSniper, Oct 24, 2015.

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

    SuperSniper

    I've been wondering how to fix this, and I've had many ideas, yet, none of them have ever worked yet.
    I am hoping that one of you members on Bukkit could help me out.

    CODE:
    Code:
        @EventHandler
        public void onInteract(PlayerInteractEvent e) {
            Player p = e.getPlayer();
            ItemStack selector = new ItemStack(Material.BEACON);
            ItemMeta selectorm = selector.getItemMeta();
            selectorm.setDisplayName(ChatColor.translateAlternateColorCodes('&', Main.getPlugin().getConfig().getString("HatSelector.Name")));
            selector.setItemMeta(selectorm);
            if(e.getItem().isSimilar(selector)) {
            if(Main.getPlugin().getConfig().getBoolean("Enable.HatSelector", true)) {
                if(p.hasPermission("HubMagic.HatSelector.Use")) {
                    p.openInventory(plugin.select);
                    }
                }
            }}
    
    ERROR:
    Code:
    [22:40:38 ERROR]: Could not pass event PlayerInteractEvent to HubMagic v1.0
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:310) ~[spigot_server.jar:git-Spigot-76236cb-c373061]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav
    a:62) ~[spigot_server.jar:git-Spigot-76236cb-c373061]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.j
    ava:502) [spigot_server.jar:git-Spigot-76236cb-c373061]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
    ava:487) [spigot_server.jar:git-Spigot-76236cb-c373061]
            at org.bukkit.craftbukkit.v1_8_R3.event.CraftEventFactory.callPlayerInte
    ractEvent(CraftEventFactory.java:227) [spigot_server.jar:git-Spigot-76236cb-c373
    061]
            at net.minecraft.server.v1_8_R3.PlayerInteractManager.interact(PlayerInt
    eractManager.java:463) [spigot_server.jar:git-Spigot-76236cb-c373061]
            at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java
    :759) [spigot_server.jar:git-Spigot-76236cb-c373061]
            at net.minecraft.server.v1_8_R3.PacketPlayInBlockPlace.a(PacketPlayInBlo
    ckPlace.java:52) [spigot_server.jar:git-Spigot-76236cb-c373061]
            at net.minecraft.server.v1_8_R3.PacketPlayInBlockPlace.a(PacketPlayInBlo
    ckPlace.java:1) [spigot_server.jar:git-Spigot-76236cb-c373061]
            at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:1
    3) [spigot_server.jar:git-Spigot-76236cb-c373061]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [
    ?:1.8.0_51]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_51]
            at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [spigot_ser
    ver.jar:git-Spigot-76236cb-c373061]
            at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:7
    15) [spigot_server.jar:git-Spigot-76236cb-c373061]
            at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:3
    74) [spigot_server.jar:git-Spigot-76236cb-c373061]
            at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:6
    54) [spigot_server.jar:git-Spigot-76236cb-c373061]
            at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java
    :557) [spigot_server.jar:git-Spigot-76236cb-c373061]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_51]
    Caused by: java.lang.NullPointerException
            at me.Archery.hubmagic.Gadgets.HatSelector.onInteract(HatSelector.java:3
    1) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0
    _51]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0
    _51]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1
    .8.0_51]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_51]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:306) ~[spigot_server.jar:git-Spigot-76236cb-c373061]
            ... 17 more
    
    Line #31 in "HatSelector":
    Code:
            if(e.getItem().isSimilar(selector)) {
    
    EDIT: The code works perfectly, but whenever I DON'T use the HatSelector inside of my hand, it shows the error.
     
  2. Offline

    Zombie_Striker

    Maybe you should check to make sure the item is not null, and check if the type is the same before using .isSimilar
     
  3. Offline

    SuperSniper

    @Zombie_Striker Same error.

    But just to clarify for people:
    The code in the post works perfectly while the HatSelector is being held in my hand, but the code makes the error whenever I DON'T have it it in my hand, so basically, it's working perfectly, but errors occur when I interact with the ground without anything in my hand.
     
  4. Offline

    mine-care

    If you've done this, then it shouldnt error.
     
  5. Offline

    bwfcwalshy Retired Staff

    @mine-care Let me fix that for you.
     
    mine-care likes this.
  6. Offline

    mine-care

  7. Offline

    SuperSniper

  8. Offline

    DoggyCode™

    Can't you just compare the booleans as so:
    Code:
    if (ItemStack#.isSimilar(ItemStack#){
    //do stuff
    } else {
    //item is treated as null (kind of?)
    {
    Instead of using object checker for a boolean (AKA "==null" or ".equals(null)").
     
Thread Status:
Not open for further replies.

Share This Page