Arrayindexoutofbounds

Discussion in 'Plugin Development' started by Marcohan, Aug 14, 2015.

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

    Marcohan

    My Code

    Code:
    if (args.length == 2) {
    
    // punish 
    
    if (cmd.getName().equalsIgnoreCase("punish")) {
    
    punish = getServer().getPlayer(args[1]);
    
    warnreason = (args[2]);
    
    // warn
    
    ItemStack item = new ItemStack(Material.WOOD_SWORD);
    
    ItemMeta meta = item.getItemMeta();
    
    meta.setDisplayName(ChatColor.GREEN + "Warn");
    
    ArrayList<String> lore = new ArrayList<String>();
    
    lore.add(pre + " Opens the Warn Option menu " + suff);
    
    meta.setLore(lore);
    
    item.setItemMeta(meta);
    
    punishoptions.setItem(2, item);
    
    // mute
    
    ItemStack item1 = new ItemStack(Material.STONE_SWORD);
    
    ItemMeta meta1 = item1.getItemMeta();
    
    meta.setDisplayName(ChatColor.GREEN + "Mute");
    
    ArrayList<String> lore1 = new ArrayList<String>();
    
    lore1.add(pre + " Opens the Mute Option Menu " + suff);
    
    meta1.setLore(lore1);
    
    punishoptions.setItem(3, item1);
    
    // player info
    
    ItemStack item2 = new ItemStack(Material.SKULL);
    
    ItemMeta meta2 = item2.getItemMeta();
    
    meta2.setDisplayName(ChatColor.GREEN + (args[0]) + " Punishment Information");
    
    ArrayList<String> lore2 = new ArrayList<String>();
    
    lore2.add(pre + " Mutes: " + suff);
    
    lore2.add(ChatColor.GRAY + " " + this.mutemap.get(punish));
    
    lore2.add(pre + " Warns: " + suff);
    
    lore2.add(ChatColor.GRAY + " " + this.warnmap.get(punish));
    
    lore2.add(pre + " Kicks: " + suff);
    
    lore2.add(ChatColor.GRAY + " " + this.kickmap.get(punish));
    
    lore2.add(pre + " Bans: " + suff);
    
    lore2.add(ChatColor.GRAY + " " + this.banmap.get(punish));
    
    meta2.setLore(lore2);
    
    punishoptions.setItem(4, item2);
    
    // kick
    
    ItemStack item3 = new ItemStack(Material.IRON_SWORD);
    
    ItemMeta meta3 = item3.getItemMeta();
    
    meta3.setDisplayName(ChatColor.GREEN + "Kick");
    
    ArrayList<String> lore3 = new ArrayList<String>();
    
    lore3.add(pre + " Opens the Kick Option Menu " + suff);
    
    meta3.setLore(lore3);
    
    punishoptions.setItem(5, item3);
    
    // ban
    
    ItemStack item4 = new ItemStack(Material.GOLD_SWORD);
    
    ItemMeta meta4 = item4.getItemMeta();
    
    meta4.setDisplayName(ChatColor.GREEN + "Ban");
    
    ArrayList<String> lore4 = new ArrayList<String>();
    
    lore4.add(pre + " Opens the Ban Option Menu " + suff);
    
    meta4.setLore(lore4);
    
    punishoptions.setItem(6, item4);
    
    ((HumanEntity) sender).openInventory(punishoptions);
    
    }
    
    error:

    Code:
    [19:09:03 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'punish' in plugin UHCPlugin v1.0
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:181) ~
        at org.bukkit.craftbukkit.v1_7_R4.CraftServer.dispatchCommand(CraftServer.java:767) ~
        at net.minecraft.server.v1_7_R4.PlayerConnection.handleCommand(PlayerConnection.java:1043) 
        at net.minecraft.server.v1_7_R4.PlayerConnection.a(PlayerConnection.java:880) 
        at net.minecraft.server.v1_7_R4.PacketPlayInChat.a(PacketPlayInChat.java:28) 
        at net.minecraft.server.v1_7_R4.PacketPlayInChat.handle(PacketPlayInChat.java:65) 
        at net.minecraft.server.v1_7_R4.NetworkManager.a(NetworkManager.java:186) 
        at net.minecraft.server.v1_7_R4.ServerConnection.c(ServerConnection.java:81) 
        at net.minecraft.server.v1_7_R4.MinecraftServer.v(MinecraftServer.java:734) 
        at net.minecraft.server.v1_7_R4.DedicatedServer.v(DedicatedServer.java:289) 
        at net.minecraft.server.v1_7_R4.MinecraftServer.u(MinecraftServer.java:584) 
        at net.minecraft.server.v1_7_R4.MinecraftServer.run(MinecraftServer.java:490) 
        at net.minecraft.server.v1_7_R4.ThreadServerApplication.run(SourceFile:628) 
    Caused by: java.lang.ArrayIndexOutOfBoundsException: 2
        at com.MarcoHan.UHCPlugin.Main.onCommand(Main.java:678) ~[?:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~
    This is happening when I execute the command /punish MarcoHan bad
     
  2. Offline

    bwfcwalshy Retired Staff

    Your trying to get the 3rd argument with warn reason but yet checked if there's only 2
     
  3. Offline

    Marcohan

    If i check it as three the command doesn't work at all, theres also only Two Arguments punish being the main Command and MarcoHan and bad being the arguments
     
  4. Offline

    Larsn

    Then add a argument?
     
  5. Offline

    teej107

    Well your current code state doesn't work either. So are you going to fix the runtime error now and debug your logic error or live with both?
     
  6. Offline

    amatokus

    @Marcohan You need to know that java and us(humans) count differently:
    • ......Java: 0 1 2 3 4 5 6 7 8 9
    • ................|.|..|.|..|.|..|.|..|..|
    • Humans: 1 2 3 4 5 6 7 8 9 10
    So here, you are asking for args[1] and args[2] to a java program, so for us this would be args[2] and args[3] ! The is why your error throws arrayindexoutofboundsexception: 2. Because 2 in java is 3 for us! but the is no argument n°3 (for us humans) !
     
  7. Offline

    teej107

    I would think he would know :p
     
  8. Offline

    amatokus

    @teej107 Oh my bad, but still, he uses "human numbers" in his if statement.
     
Thread Status:
Not open for further replies.

Share This Page