ArrayIndexOutOfBoundsException: 1

Discussion in 'Spigot Plugin Development' started by DecisionsYT, May 22, 2016.

  1. Hey!

    I'm creating a server selector plugin.

    When doing /serverselector, I get an error.

    CODE:
    Code (Text):
    package me.DecisionsYT;

    import java.util.Arrays;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.java.JavaPlugin;

    public class Main extends JavaPlugin {

        @Override
        public void onEnable() {
            getLogger().info("UberSelector Has Been Enabled!");
         
         
         
        }


        @Override
        public void onDisable() {
            getLogger().info("UberSelector Has Been Disabled!");
        }
     
        Inventory inv = Bukkit.createInventory(null, getConfig().getInt("GUI-Rows"), ChatColor.translateAlternateColorCodes('&', getConfig().getString("GUI-Name")));
        Material Server1Material = Material.getMaterial(getConfig().getString("Server1-Material"));
        Material Server2Material = Material.getMaterial(getConfig().getString("Server2-Material"));
        Material Server3Material = Material.getMaterial(getConfig().getString("Server3-Material"));
        Material Server4Material = Material.getMaterial(getConfig().getString("Server4-Material"));
        Material Server5Material = Material.getMaterial(getConfig().getString("Server5-Material"));
     
        public boolean onCommand(CommandSender sender, Command command, String Label, String[] args) {
            if (command.getName().equalsIgnoreCase("ServerSelector")) {
             
             
             
                  ItemStack Server1 = new ItemStack(Server1Material, getConfig().getInt("Server1-Amount"));
                  ItemMeta Server1meta = Server1.getItemMeta();
                  Server1meta.setDisplayName(ChatColor.translateAlternateColorCodes('&', getConfig().getString("Server1-Name")));
                  Server1meta.setLore(Arrays.asList(new String[] { ChatColor.translateAlternateColorCodes('&', getConfig().getString("Server1-Lore")) }));
                  Server1.setItemMeta(Server1meta);

                  ItemStack Server2 = new ItemStack(Server2Material, getConfig().getInt("Server2-Amount"));
                  ItemMeta Server2meta = Server2.getItemMeta();
                  Server2meta.setDisplayName(ChatColor.translateAlternateColorCodes('&', getConfig().getString("Server2-Name")));
                  Server2meta.setLore(Arrays.asList(new String[] { ChatColor.translateAlternateColorCodes('&', getConfig().getString("Server2-Lore")) }));
                  Server2.setItemMeta(Server2meta);
               
                  ItemStack Server3 = new ItemStack(Server3Material, getConfig().getInt("Server3-Amount"));
                  ItemMeta Server3meta = Server3.getItemMeta();
                  Server3meta.setDisplayName(ChatColor.translateAlternateColorCodes('&', getConfig().getString("Server3-Name")));
                  Server3meta.setLore(Arrays.asList(new String[] { ChatColor.translateAlternateColorCodes('&', getConfig().getString("Server3-Lore")) }));
                  Server3.setItemMeta(Server3meta);
               
                  ItemStack Server4 = new ItemStack(Server4Material, getConfig().getInt("Server4-Amount"));
                  ItemMeta Server4meta = Server4.getItemMeta();
                  Server4meta.setDisplayName(ChatColor.translateAlternateColorCodes('&', getConfig().getString("Server4-Name")));
                  Server4meta.setLore(Arrays.asList(new String[] { ChatColor.translateAlternateColorCodes('&', getConfig().getString("Server4-Lore")) }));
                  Server4.setItemMeta(Server4meta);
               
                  ItemStack Server5 = new ItemStack(Server5Material, getConfig().getInt("Server5-Amount"));
                  ItemMeta Server5meta = Server5.getItemMeta();
                  Server5meta.setDisplayName(ChatColor.translateAlternateColorCodes('&', getConfig().getString("Server5-Name")));
                  Server5meta.setLore(Arrays.asList(new String[] { ChatColor.translateAlternateColorCodes('&', getConfig().getString("Server5-Lore")) }));
                  Server5.setItemMeta(Server5meta);
               
                  inv.setItem(getConfig().getInt("Server1-Slot"), Server1);
                  inv.setItem(getConfig().getInt("Server2-Slot"), Server2);
                  inv.setItem(getConfig().getInt("Server3-Slot"), Server3);
                  inv.setItem(getConfig().getInt("Server4-Slot"), Server4);
                  inv.setItem(getConfig().getInt("Server5-Slot"), Server5);
               
                  Player player = (Player) sender;
                  player.openInventory(inv);
               
            }
            return true;
         
            }
         
     
        }
       
    STACK TRACE:

    Code (Text):
    22.05 15:04:55 [Server] INFO ... 15 more
    22.05 15:04:55 [Server] INFO at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot-BT41-1.8.8.jar:git-Spigot-f94fe8f-d27e6d0]
    22.05 15:04:55 [Server] INFO at me.DecisionsYT.Main.onCommand(Main.java:74) ~[?:?]
    22.05 15:04:55 [Server] INFO at org.bukkit.craftbukkit.v1_8_R3.inventory.CraftInventory.setItem(CraftInventory.java:79) ~[spigot-BT41-1.8.8.jar:git-Spigot-f94fe8f-d27e6d0]
    22.05 15:04:55 [Server] INFO at org.bukkit.craftbukkit.v1_8_R3.inventory.CraftInventoryCustom$MinecraftInventory.setItem(CraftInventoryCustom.java:105) ~[spigot-BT41-1.8.8.jar:git-Spigot-f94fe8f-d27e6d0]
    22.05 15:04:55 [Server] INFO Caused by: java.lang.ArrayIndexOutOfBoundsException: 1
    22.05 15:04:55 [Server] INFO at java.lang.Thread.run(Thread.java:745) [?:1.8.0_60]
    22.05 15:04:55 [Server] INFO at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:556) [spigot-BT41-1.8.8.jar:git-Spigot-f94fe8f-d27e6d0]
    22.05 15:04:55 [Server] INFO at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:653) [spigot-BT41-1.8.8.jar:git-Spigot-f94fe8f-d27e6d0]
    22.05 15:04:55 [Server] INFO at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [spigot-BT41-1.8.8.jar:git-Spigot-f94fe8f-d27e6d0]
    22.05 15:04:55 [Server] INFO at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:714) [spigot-BT41-1.8.8.jar:git-Spigot-f94fe8f-d27e6d0]
    22.05 15:04:55 [Server] INFO at net.minecraft.server.v1_8_R3.SystemUtils.a(SystemUtils.java:19) [spigot-BT41-1.8.8.jar:git-Spigot-f94fe8f-d27e6d0]
    22.05 15:04:55 [Server] INFO at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_60]
    22.05 15:04:55 [Server] INFO at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:1.8.0_60]
    22.05 15:04:55 [Server] INFO at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [spigot-BT41-1.8.8.jar:git-Spigot-f94fe8f-d27e6d0]
    22.05 15:04:55 [Server] INFO at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:1) [spigot-BT41-1.8.8.jar:git-Spigot-f94fe8f-d27e6d0]
    22.05 15:04:55 [Server] INFO at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:45) [spigot-BT41-1.8.8.jar:git-Spigot-f94fe8f-d27e6d0]
    22.05 15:04:55 [Server] INFO at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:997) [spigot-BT41-1.8.8.jar:git-Spigot-f94fe8f-d27e6d0]
    22.05 15:04:55 [Server] INFO at net.minecraft.server.v1_8_R3.PlayerConnection.handleCommand(PlayerConnection.java:1162) [spigot-BT41-1.8.8.jar:git-Spigot-f94fe8f-d27e6d0]
    22.05 15:04:55 [Server] INFO at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:640) ~[spigot-BT41-1.8.8.jar:git-Spigot-f94fe8f-d27e6d0]
    22.05 15:04:55 [Server] INFO at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot-BT41-1.8.8.jar:git-Spigot-f94fe8f-d27e6d0]
    22.05 15:04:55 [Server] INFO at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot-BT41-1.8.8.jar:git-Spigot-f94fe8f-d27e6d0]
    22.05 15:04:55 [Server] INFO org.bukkit.command.CommandException: Unhandled exception executing command 'serverselector' in plugin UberSelector v1.0
    22.05 15:04:55 [Server] ERROR null
    22.05 15:04:55 [Server] INFO DecisionsYT issued server command: /serverselector
     
  2. What's line 74 of your Main class?
     
  3. Forgot to add that to the post xD

    Code (Text):
    inv.setItem(getConfig().getInt("Server1-Slot"), Server1);
     
  4. I think your getCofnig().getInt("Server1-Slot") is higher than there are places in your inventory.
     
  5. Inventory space: 9
    Slot: 1
     
  6. Any idea?
     
  7. Clearly the slot is 1. The only way I can think of this throwing an OutOfBoundsException is see what the size of the Inventory you made it. Print the size of it just before you set the item.
     
  8. The config.yml is this. So it seems weird of this error.

    Code (Text):
    # UberSelector By DecisionsYT
    # Version 1.0

    # GUI-Slots must be divisible by 9
    GUI-Name: '&7[&b&lSERVERS&7]'
    GUI-Slots: 18

    Server1-Material: 'TNT'
    Server1-Amount: 1
    Server1-Name: '&7[&b&lFACTIONS&7]'
    Server1-Lore: '&bClick this to teleport to the &3Factions &bserver.'
    Server1-Slot: 1

    Server2-Material: 'GRASS'
    Server2-Amount: 1
    Server2-Name: '&7[&b&lSURVIVAL&7]'
    Server2-Lore: '&bClick this to teleport to the &3Survival &bserver.'
    Server2-Slot: 2

    Server3-Material: 'DIAMOND'
    Server3-Amount: 1
    Server3-Name: '&7[&b&lSKYWARS&7]'
    Server3-Lore: '&bClick this to teleport to the &3Skywars &bserver.'
    Server3-Slot: 3

    Server4-Material: 'SNOW_BALL'
    Server4-Amount: 1
    Server4-Name: '&7[&b&lSPLEEF&7]'
    Server4-Lore: '&bClick this to teleport to the &3Spleef &bserver.'
    Server4-Slot: 4

    Server5-Material: 'DIAMOND_SWORD'
    Server5-Amount: 1
    Server5-Name: '&7[&b&lKIT-PVP&7]'
    Server5-Lore: '&bClick this to teleport to the &3Kit-PvP &bserver.'
    Server5-Slot: 5
     
  9. He means, checking the size of inventory before you put the item in it, not the position.
     
  10. #

    Look: GUI-Slots: 18
     
  11. The slot is 1 in config. getInt will return -1 if the Int was not found inside of config. So print out the Int to make sure it is 1 and not -1.
     
  12. aight
     
  13. Actually the index out of bounds shows it being 1 not -1 so ignore that sorry :p
     
  14. Ok, any suggestions from that??
     
  15. One question: Why is your stacktrace upside-down?
     
  16. Create the inventory in the command so it's a unique inventory to the player otherwise it's gonna bug out when multiple people run the command.

    Like this:

    Inventory inv;

    In command:

    inv = Bukkit.create..
     
  17. Multicraft
     
  18. Yeah I think your console is upside down.

    So at first it shows null. Null can be anything in there, setting your name, the inventory was null, anything in there. Then it maybe caused the inventory to be created wrongly somehow. I would print everything and figure out what is null. Print the inventory, print all the getStrings then see which is returning null.
     
  19. Doesn't solve it