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();

                    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
    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( ~[?:1.8.0_201]
            at java.util.ArrayList.get( ~[?:1.8.0_201]
            at ~[?:?]
            at ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_201]
            at sun.reflect.NativeMethodAccessorImpl.invoke( ~[?:1.8.0_201]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke( ~[?:1.8.0_201]
            at java.lang.reflect.Method.invoke( ~[?:1.8.0_201]
            at$1.execute( ~[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):
    Yet you're using those indices to get something from:
    Code (Java):
    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...