Gregoirelpv
Gregoirelpv

Reputation: 112

Error ArrayIndexOutOfBoundsException Java/Bukkit

My server console send me back this error: java.lang.ArrayIndexOutOfBoundsException That's a simplest version of my code :

int[] test = new int[] {0,1,2,3,4,5,6,7,8,9,14,15,17,18,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45};
for (int value : test) {
    System.out.println(value, obj);
}

That's my full code for bukkit/spigot dev:

    //In a event
    voidObj(new int[] {0,1,2,3,4,5,6,7,8,9,14,15,17,18,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45}, parcoursConfigGUI);

    //Itemconstructor
    private void voidObj(int[] position, Inventory inventory) {
        ItemStack obj = new ItemStack(Material.BLACK_STAINED_GLASS_PANE, 1);
        ItemMeta meta = obj.getItemMeta();
        meta.setDisplayName(null);
        meta.addItemFlags(ItemFlag.HIDE_ATTRIBUTES);
        obj.setItemMeta(meta);

        for (int value : position) {
            inventory.setItem(value, obj);
        }
    }

Upvotes: 0

Views: 342

Answers (1)

Gesit
Gesit

Reputation: 539

An Inventory does always have a size, which is a multiple of 9. You are iterating from 0-45 this sums up to 46 slots. 46 isn't a multiple of 9. My assumption is that you want to iterate from 0-44 so delete 45 from the int array:

new int[] {0,1,2,3,4,5,6,7,8,9,14,15,17,18,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44}

You should think about using a for loop to accomplish the same thing:

for (int value = 0; i<inventory.getSize();value++){
    inventory.setItem(value, obj);
}

Upvotes: 1

Related Questions