Help ! ArrayIndexOutOfBoundsException

Discussion in 'Spigot Plugin Development' started by AugusteRodin, Jun 9, 2018.

  1. Here is the error code;
    Code (Text):
    ERROR]: Could not pass event InventoryClickEvent to ItemMaker v1.0
    org.bukkit.event.EventException: null
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot-1.12.jar:git-Spigot-596221b-9a1fc1e]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot-1.12.jar:git-Spigot-596221b-9a1fc1e]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:499) [spigot-1.12.jar:git-Spigot-596221b-9a1fc1e]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:484) [spigot-1.12.jar:git-Spigot-596221b-9a1fc1e]
            at net.minecraft.server.v1_12_R1.PlayerConnection.a(PlayerConnection.java:1877) [spigot-1.12.jar:git-Spigot-596221b-9a1fc1e]
            at net.minecraft.server.v1_12_R1.PacketPlayInWindowClick.a(SourceFile:33) [spigot-1.12.jar:git-Spigot-596221b-9a1fc1e]
            at net.minecraft.server.v1_12_R1.PacketPlayInWindowClick.a(SourceFile:10) [spigot-1.12.jar:git-Spigot-596221b-9a1fc1e]
            at net.minecraft.server.v1_12_R1.PlayerConnectionUtils$1.run(SourceFile:13) [spigot-1.12.jar:git-Spigot-596221b-9a1fc1e]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:?]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:?]
            at net.minecraft.server.v1_12_R1.SystemUtils.a(SourceFile:46) [spigot-1.12.jar:git-Spigot-596221b-9a1fc1e]
            at net.minecraft.server.v1_12_R1.MinecraftServer.D(MinecraftServer.java:747) [spigot-1.12.jar:git-Spigot-596221b-9a1fc1e]
            at net.minecraft.server.v1_12_R1.DedicatedServer.D(DedicatedServer.java:405) [spigot-1.12.jar:git-Spigot-596221b-9a1fc1e]
            at net.minecraft.server.v1_12_R1.MinecraftServer.C(MinecraftServer.java:678) [spigot-1.12.jar:git-Spigot-596221b-9a1fc1e]
            at net.minecraft.server.v1_12_R1.MinecraftServer.run(MinecraftServer.java:576) [spigot-1.12.jar:git-Spigot-596221b-9a1fc1e]
            at java.lang.Thread.run(Unknown Source) [?:?]
    Caused by: java.lang.ArrayIndexOutOfBoundsException
    >
    My InventoryClickEvent ;
    Code (Text):
    // Scroll List MENU
        @EventHandler
        fun onScrollClick(event: InventoryClickEvent) {
            val inventory = event.inventory
            val clicked = event.currentItem
            val player = event.whoClicked as Player

            try {
                if (inventory.getName() == "§c§lScroll List" && (clicked == null || clicked.itemMeta == null || event.getCurrentItem().getType() == Material.AIR)) {
                    return
                } else if (inventory.getName() == "§c§lScroll List" && clicked.itemMeta.displayName.contains("§6Upgrade Scroll") && (event.isLeftClick() || event.isRightClick())) {
                    player.closeInventory()
                    CommandAnvil().onLowClass(player)
                    return
                } else if (inventory.getName() == "§c§lScroll List" && clicked.itemMeta.displayName.contains("§6Middle Class") && (event.isLeftClick() || event.isRightClick())) {
                    player.closeInventory()
                    CommandAnvil().onMiddleClass(player)
                    return
                } else if (inventory.getName() == "§c§lScroll List" && clicked.itemMeta.displayName.contains("§6Blessed Upgrade") && (event.isLeftClick() || event.isRightClick())) {
                    player.closeInventory()
                    CommandAnvil().onHighClass(player)
                    return
                }
            } catch (ex: NullPointerException) {

            }


        }

    My Main Scroll List Method;
    Code (Text):
    // Anvil Scroll Method
        public fun anvilScroll(player: Player) {
            // START
            val scrollinv = Bukkit.createInventory(null, 9, "§c§lScroll List")

            // OPTIONS
            val duvar = ItemStack(Material.STAINED_GLASS_PANE, 1, 15)
            val duvarmeta = duvar.itemMeta
            val loreduvar = ArrayList<String>()

            duvarmeta.displayName = ("§c*")

            duvarmeta.addItemFlags(ItemFlag.HIDE_ATTRIBUTES)
            duvarmeta.addItemFlags(ItemFlag.HIDE_ENCHANTS)
            // Setleme komudu
            duvarmeta.setLore(loreduvar)
            duvar.setItemMeta(duvarmeta)

            // LOW CLASS
            val lowclass = ItemStack(Material.PAPER, 1)
            val lowclassmeta = lowclass.itemMeta
            val lorelowclass = ArrayList<String>()

            lorelowclass.add("§7* Upgrades your item's grade")

            lowclassmeta.displayName = ("§6Upgrade Scroll §e(Low Class Item)")

            lowclassmeta.addItemFlags(ItemFlag.HIDE_ATTRIBUTES)
            lowclassmeta.addItemFlags(ItemFlag.HIDE_ENCHANTS)
            // LOW CLASS FINAL ( SET )
            lowclassmeta.setLore(lorelowclass)
            lowclass.setItemMeta(lowclassmeta)
    //---------------------------------------------------------------
            // MİDDLE CLASS
            val middleclass = ItemStack(Material.PAPER, 1)
            val middleclassmeta = middleclass.itemMeta
            val loremiddleclass = ArrayList<String>()

            loremiddleclass.add("§7* Upgrades your item's grade")

            middleclassmeta.displayName = ("§6Middle Class Scroll §a(Middle Class Item)")

            middleclassmeta.addItemFlags(ItemFlag.HIDE_ATTRIBUTES)
            middleclassmeta.addItemFlags(ItemFlag.HIDE_ENCHANTS)
            // LOW CLASS FINAL ( SET )
            middleclassmeta.setLore(loremiddleclass)
            middleclass.setItemMeta(middleclassmeta)
            //---------------------------------------------------------------
            // HIGH CLASS
            val highclass = ItemStack(Material.PAPER, 1)
            val highclassmeta = middleclass.itemMeta
            val lorehighclass = ArrayList<String>()

            lorehighclass.add("§7* Upgrades your item's grade")

            highclassmeta.displayName = ("§6Bless Upgrade Scroll §c(High Class Item)")

            highclassmeta.addItemFlags(ItemFlag.HIDE_ATTRIBUTES)
            highclassmeta.addItemFlags(ItemFlag.HIDE_ENCHANTS)
            // LOW CLASS FINAL ( SET )
            highclassmeta.setLore(lorehighclass)
            highclass.setItemMeta(highclassmeta)

            scrollinv.setItem(0, duvar)
            scrollinv.setItem(1, duvar)
            scrollinv.setItem(2, duvar)
            scrollinv.setItem(3, lowclass)
            scrollinv.setItem(4, middleclass)
            scrollinv.setItem(5, highclass)
            scrollinv.setItem(6, duvar)
            scrollinv.setItem(7, duvar)
            scrollinv.setItem(8, duvar)


            //FINAL
            player.openInventory(scrollinv)
        }
     
     
    #1 AugusteRodin, Jun 9, 2018
    Last edited: Jun 9, 2018
  2. Okay.. give us the class that this relates to where you listen for the InventoryClickEvent.
     
  3. Did you write a plugin that is giving you this error? Or do you just run a server and then this showed up?
     
  4. Can you provide any code?
     
  5. yes when i click the item from event inventory it give this error
     
  6. Yes what? And it may be helpful for us to be able to look into the code of the plugin causing this error.
     
  7. i add my event and my method
     
    • Useful Useful x 1
  8. i add InventoryClickEvent of my plugin and method
     
  9. What is SimplePluginManager?
     
  10. May be because your using 'or' which makes the second check throwing error.
    Code (Text):
    clicked == null || clicked.itemMeta == null
    //edit
    You never checked if clicked.itemMeta.displayName == null
    Code (Text):
    clicked.itemMeta.displayName.contains("§6Upgrade Scroll")
    //edit 2
    Also why do you use #contains() instead of ==?
    Code (Text):
    clicked.itemMeta.displayName == "§6Upgrade Scroll"
     
    #10 NicoNeko, Jun 9, 2018
    Last edited: Jun 9, 2018
    • Winner Winner x 1
  11. Learn java and you would be able to fix this problem within 3 seconds.
    You are trying to acces some kind of array but with a too big index. Lets say you have 2 apples and you try to grab number 3, of course that won't work.

    Also why do you use try {} catch{} for a nullpointerexception. It would be way cleaner to setup checks for those.

    I do not see how that relates to this issue. This is not a NPE. He even catches those.
     
  12. I try but still doesn't working
     
  13. Is not, but avoiding them is good practice =)
    Also I don't know, maybe the error is caused by kotlin itself.
     
  14. I do not understand the code since Kotlin is a bit basic. I don't understand why the null pointer is occuring. I believe it has something to do with the first three lines in the class:
    Code (Java):
    val inventory = event.inventory
    val clicked = event.currentItem
    val player = event.whoClicked as Player
    One of these might not be set, most probably the Item.
    It has to be down to these as you are catching the rest of the code. Try to put the whole code in the catch clause and see if that changes anything,
    console wise.
     
  15. I don't know if you know just normal Java yet, but I would recommend you learning that first if you haven't.
    Will probably help you in the long run :)
     
  16. Also consider learning C++, Just saying. The algorithms are much faster, and you will be able to learn stuff you will require in Java. Java is C++ without pointers.
     
  17. Also....

    How are you going to make spigot plugins using C++