ArrayIndexOutOfBoundsException

Discussion in 'Spigot Plugin Development' started by Blubo, Jan 7, 2020.

  1. So today I wanted to write a "simple" shop plugin but I ran into this hell of an exception and I don't know how to fix it.

    The line in question is:
    Code (Text):
    if (item_names.get(i).contains("/")) {
    The sorrounding code is following:
    Code (Text):

    for (int i = 0; i < ItemManager.items.get(name).size(); i++) {
                if (item_names.get(i).contains("/")) {
                String[] split = item_names.get(i).split("/");
                String[] split2 = items_pre.get(item_names.get(i)).split("/");
                    List<String> test = new ArrayList<String>();

                    test.add("§7§lPreis: §c" + split2[0]);
                    test.add("§7§lVerkaufen fuer: §c" + split2[1]);

                    ItemStack item = new ItemStack(Material.valueOf(split[0]), 1, Short.valueOf(split[1]));
                    ItemMeta item_m = item.getItemMeta();
                    item_m.setLore(test);
                    item.setItemMeta(item_m);

                    inv.setItem(i, item);
    To explain this - so you can better undstand what I'm trying to do.

    I have an ItemManager which has a few lists in it (including item_names (ArrayList) and items_pre (HashMap))
    Every entry in item_names is just the name of the variable of the item in Material#
    Example: Entry in my list: IRON_INGOT
    Result in Material#: Material.IRON_INGOT
    items_pre are just the Items with the buy / sell values and metadatas as well as the Material# variable
    attached.
    Example: Entry in my list: Key: STONE/1 <- the 1 being the metadata (in this case granite)
    Variable: 25(buy value)/15(sell value)
    In this case the list item_names has 17 Entrys.

    The Exception is following:
    Code (Text):

    Caused by: java.lang.IndexOutOfBoundsException: Index: 17, Size: 17
            at java.util.ArrayList.rangeCheck(ArrayList.java:657) ~[?:1.8.0_201]
            at java.util.ArrayList.get(ArrayList.java:433) ~[?:1.8.0_201]
            at me.blubo.development.events.InventoryClickEventHandler.prepareInv(InventoryClickEventHandler.java:121) ~[?:?]
            at me.blubo.development.events.InventoryClickEventHandler.onClick(InventoryClickEventHandler.java:86) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_201]
            at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_201]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_201]
            at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_201]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
            ... 15 more
    If any questions about my code remain - please feel free to ask.
    Thanks for reading and I hope you can help me.
     
  2. The array/collections have index as 0 to size - 1, so if your list is of size 17 the last index is 16
     
  3. What I'm seeing is that you're iterating through indices limited to the size of this Collection:
    Code (Java):
    ItemManager.items.get(name).size()
    Yet you're using those indices to get something from:
    Code (Java):
    item_names
    And there is no evidence anywhere that those are the same Collection.
    Let me know if that helped.
     
  4. They are not the same lists but they do have the excact same amount of entries within them (because I manually entry them into said lists)
     
  5. Please make sure of that before you do it:
    If lis1.size() != list2.size() print...