OutOfBoundsException

Discussion in 'Plugin Development' started by Welite, Oct 21, 2013.

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

    Welite

    Hi I am making a GUI in inventory but I got problem, I want 6x9 inventory to generate after click on the lost on the first gui, but It gives me this error.


    My code:
    Code:java
    1. if (ce.getSlot() == 3) {
    2. pl.closeInventory();
    3. Inventory x105 = Bukkit.createInventory(null, 54, "MyINV");
    4. int i;
    5. for (i = 0; i <= 54; i++) {
    6.  
    7.  
    8. ItemStack slot99 = new ItemStack(Material.WATER, 1);
    9. ItemMeta meta99 = slot99.getItemMeta();
    10. meta99.setDisplayName(ChatColor.AQUA + "Water Field");
    11. slot99.setItemMeta(meta99);
    12. x105.setItem(i, slot99); // the error is on this line
    13.  
    14.  
    15.  
    16.  
    17. }
    18. pl.openInventory(x105);
    19.  
    20. }




    Error is exactly there: x105.setItem(i, slot99); // the error is on this line

    It seems like it does not accept the variable i, but I dont know why.
    Can someone help me please ?
     
  2. mostly, its usefull to give us the error message. that way we don't need to look through you code to see whats the wrong part.

    When typing this, I noticed you used:
    Code:
    for (i = 0; i <= 54; i++) {
    this will loop over an array of the size of 55 (not 54 because pf <=), causing an ArrayOutOfBoundsException to thrown at the last loop.

    The solution if to change the line:
    Code:
    for (i = 0; i <= 54; i++) {
    into the following
    Code:
    for (i = 0; i < 54; i++) {
    EDIT: didn't looked at the title, it was the same error as I think it was....
     
Thread Status:
Not open for further replies.

Share This Page