InventoryClickEvent - Error

Discussion in 'Plugin Development' started by BullstrikeMC, Mar 26, 2016.

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

    BullstrikeMC

    Hello,

    I am making a plugin and when my player clicks inside of a custom inventory it works fine but if they are still in the inventory and click outside of the inventory it gives me this error.

    Code:
    [21:57:21] [Server thread/ERROR]: Could not pass event InventoryClickEvent to Hub v1.0
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:310) ~[spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:1630) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.PacketPlayInWindowClick.a(SourceFile:31) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.PacketPlayInWindowClick.a(SourceFile:9) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
        at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_77]
        at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_77]
        at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:715) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_77]
    Caused by: java.lang.NullPointerException
        at com.bullstrikemc.hub.Listeners.onSuitsClick(Listeners.java:1004) ~[?:?]
        at sun.reflect.GeneratedMethodAccessor11.invoke(Unknown Source) ~[?:?]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_77]
        at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_77]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
        ... 15 more
    why... ?

    Inventory Click Event:
    Code:
        @EventHandler
        public void onCollectiblesClick(InventoryClickEvent e)
        {
            Player p = (Player)e.getWhoClicked();
            if (e.getSlotType().equals(InventoryType.SlotType.OUTSIDE)) {
                return;
            }
            if (!e.getInventory().getName().equalsIgnoreCase(Interface.collectibles.getName())) {
                return;
            }
            if (e.getCurrentItem().getType() == Material.NETHER_STAR)
            {
                e.setCancelled(true);
                p.playSound(p.getLocation(), Sound.CLICK, 0.5F, 3F);
                p.openInventory(Interface.particles);
            }
            else if (e.getCurrentItem().getType() == Material.DIAMOND_HELMET)
            {
                e.setCancelled(true);
                p.playSound(p.getLocation(), Sound.CLICK, 0.5F, 3F);
                p.openInventory(Interface.hats);
            }
            else if (e.getCurrentItem().getType() == Material.GOLD_LEGGINGS)
            {
                e.setCancelled(true);
                p.playSound(p.getLocation(), Sound.CLICK, 0.5F, 3F);
                p.openInventory(Interface.suits);
            }
            else if (e.getCurrentItem().getType() == Material.PISTON_BASE)
            {
                e.setCancelled(true);
                p.playSound(p.getLocation(), Sound.CLICK, 0.5F, 3F);
                p.openInventory(Interface.gadgets);
            }
        }
    Any way i can fix this error?

    Thank you and have a nice day! :D
     
  2. @BullstrikeMC
    You should include the line that is causing the error in your post.
    Code:
    Caused by: java.lang.NullPointerException
        at com.bullstrikemc.hub.Listeners.onSuitsClick(Listeners.java:1004) ~[?:?]
    Line 1004 in Listeners class. My guess would be this line:
    Code:
    if (e.getCurrentItem().getType() == Material.NETHER_STAR)
    You should first make sure that the getCurrentItem is not null before checking its type.
     
  3. Offline

    BullstrikeMC

    I'm so very confused :|
    can u show me an example please ?

    u said "You should first make sure that the getCurrentItem is not null before checking its type."
    like this:
    Code:
    if (e.getCurrentItem() != null && e.getCurrentItem().getType() == Material.NETHER_STAR {
     
    Last edited: Mar 26, 2016
  4. StarRocket likes this.
  5. Offline

    BullstrikeMC

    ok... i will try to fix them but i'm not sure i will succeed because i try a lot of methods :(

    Thank you :)
     
  6. Offline

    mcdorli

    Wait a minute. Why the f*ck do you have 1004 lines in a class, if I may ask. It should be at max 200.
     
  7. Offline

    BullstrikeMC

    Because i have:
    particles (0ne page)
    gadgets (two pages)
    suits (one page)
    hats (three pages)
    :)
     
  8. Offline

    timtower Administrator Administrator Moderator

  9. Offline

    mcdorli

    Then break it up into multiple classes, this is what OOP means. Having code in separate classes and organized.

    I also noticed after phasing trough ypur code, that you are violating D.R.Y. (Don't repeat yourself), your code is W.E.T. (Wrotr everything twice OR We enjoy typing). Basically there are multiple lines in ypur code, wich are repeating, the code you posted (35 lines) can be cut down to 15-20 lines.
     
  10. Offline

    BullstrikeMC

    i know is easy to split but I never managed to tie them together
     
  11. Offline

    mcdorli

    Then:
    1.: Go and learn how to wrote proper OOP programs
    2.: Learn what switches are (in programming)
    3.: Learn the difference bstween WET and DRY programming
    4.: optimize your code

    As I told you, you could cut down 25% of your code, that's a lot, you would get 750 libes out of 1000. If possible, post your code on pastebin, and make it expire in a week or two, so I can chdck it out, and point out the problems (believe me, I have better things to do than stealing code)

    And please, don't tell me you have a class named Interface
     
  12. Offline

    BullstrikeMC

    yep i have a class named Interface :))
     
  13. Offline

    mcdorli

    Ok then learn what reserved keywords are and why don't use them as variable names (hint: some of them are: boolean, int, final, static, enum, float, etc.)
     
  14. Offline

    BullstrikeMC

    yeah

    so... any way how can i fix this problem?

    i try to fix them by upgrading my plugin to 1.9 but same...

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Mar 27, 2016
Thread Status:
Not open for further replies.

Share This Page