Solved ArrayIndexOutOfBoundsException, am I doing it wrong?

Discussion in 'Plugin Development' started by AdamDev, Oct 1, 2017.

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

    AdamDev

    So, I did post not that long ago about that it wasn't working for my list, now a different problem.

    When looping through the list it keeps giving me an ArrayIndexOutOfBoundsException.

    Heres the error:
    Code:
    [04:44:19 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'bm' in plugin BlackMarket v1.0.9         
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[craftbukkit-1.11.2.jar:git-Bukkit-6e3cec8]         
    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:140) ~[craftbukkit-1.11.2.jar:git-Bukkit-6e3cec8]         
    at org.bukkit.craftbukkit.v1_11_R1.CraftServer.dispatchCommand(CraftServer.java:629) ~[craftbukkit-1.11.2.jar:git-Bukkit-6e3cec8]         
    at net.minecraft.server.v1_11_R1.PlayerConnection.handleCommand(PlayerConnection.java:1295) [craftbukkit-1.11.2.jar:git-Bukkit-6e3cec8]         
    at net.minecraft.server.v1_11_R1.PlayerConnection.a(PlayerConnection.java:1155) [craftbukkit-1.11.2.jar:git-Bukkit-6e3cec8]         
    at net.minecraft.server.v1_11_R1.PacketPlayInChat.a(SourceFile:37) [craftbukkit-1.11.2.jar:git-Bukkit-6e3cec8]         
    at net.minecraft.server.v1_11_R1.PacketPlayInChat.a(SourceFile:9) [craftbukkit-1.11.2.jar:git-Bukkit-6e3cec8]         
    at net.minecraft.server.v1_11_R1.PlayerConnectionUtils$1.run(SourceFile:13) [craftbukkit-1.11.2.jar:git-Bukkit-6e3cec8]         
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:1.8.0_112]         
    at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_112]         
    at net.minecraft.server.v1_11_R1.SystemUtils.a(SourceFile:46) [craftbukkit-1.11.2.jar:git-Bukkit-6e3cec8]      at net.minecraft.server.v1_11_R1.MinecraftServer.D(MinecraftServer.java:695) [craftbukkit-1.11.2.jar:git-Bukkit-6e3cec8]         
    at net.minecraft.server.v1_11_R1.DedicatedServer.D(DedicatedServer.java:360) [craftbukkit-1.11.2.jar:git-Bukkit-6e3cec8]         
    at net.minecraft.server.v1_11_R1.MinecraftServer.C(MinecraftServer.java:650) [craftbukkit-1.11.2.jar:git-Bukkit-6e3cec8]         
    at net.minecraft.server.v1_11_R1.MinecraftServer.run(MinecraftServer.java:554) [craftbukkit-1.11.2.jar:git-Bukkit-6e3cec8]         
    at java.lang.Thread.run(Thread.java:745) [?:1.8.0_112] 
    Caused by: java.lang.ArrayIndexOutOfBoundsException
    
    But this doesn't give me where the error is.

    Heres where it loops through it:
    Open.java:
    Code:
    package com.gc.blackmarket.commands;
    
    import java.util.ArrayList;
    
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    
    import com.gc.blackmarket.Core;
    import com.gc.blackmarket.inventorys.Blackmarket;
    
    public class Open implements CommandExecutor {
    
        String igprefix = "§7[§0BlackMarket§7] §8> §r";
        Core plugin = Core.getInstance();
        Help help = new Help();
        Blackmarket bm = new Blackmarket();
       
        public boolean onCommand(CommandSender sender, Command cmd, String l, String[] a) {
            if (!(sender instanceof Player)) {
                sender.sendMessage(ChatColor.RED+"You must be a 'Player' to run this command!");
                return true;
            }
            Player p = (Player) sender;
            int length = a.length; 
            if (cmd.getName().equalsIgnoreCase("bm")) {
                if (length == 0) {
                    if (plugin.getConfig().getString("Blackmarket.inventory") == null) {
                        p.sendMessage(igprefix+ChatColor.RED+"There hasn't been a item sold! Someone must sell a item before you can open it!");
                        return true;
                    } else if (plugin.getConfig().getString("Blackmarket.inventory") != null) {
                    for (int i = 0;i<=bm.blackMarket().getContents().length;i++) {
                        for (int x = 0;x<Core.players.size();x++) {  // << Loops through the list HERE
                            if (x == Core.players.size() || i == 45) {
                                break;
                            }
                            Material mat = Material.getMaterial(plugin.getConfig().getString("Blackmarket.inventory."+Core.players.get(x)+".selling.item"));
                            int amot = plugin.getConfig().getInt("Blackmarket.inventory."+Core.players.get(x)+".selling.amount");
                            ItemStack is = new ItemStack(mat,amot);
                            ItemMeta ism = is.getItemMeta();
                            ArrayList<String> isl = new ArrayList<String>();
                            int price = plugin.getConfig().getInt("Blackmarket.inventory."+Core.players.get(x)+".selling.price");
                            int amt = plugin.getConfig().getInt("Blackmarket.inventory."+Core.players.get(x)+".selling.amount");
                            int all = price * amt;
                            ism.setDisplayName("§b"+plugin.getConfig().getString("Blackmarket.inventory."+Core.players.get(x)+".selling.item"));
                            isl.add("§bPrice: §e"+price);
                            isl.add("§bAmount: §e"+amt);
                            isl.add("§bAll: §e"+all);
                            ism.setLore(isl);
                            is.setItemMeta(ism);
                            bm.blackMarket().setItem(i, is);
                        }
                    }
                    p.sendMessage(igprefix+ChatColor.YELLOW+"Opening...");
                    p.openInventory(bm.blackMarket());
                    return true;
                }
            }
                if (length == 1) {
                    help.tooManyArgs(p, "/bm");
                    return true;
                }
            }
            return true;
        }
    
    }
    
    
    
    The ArrayList is in the main class (Core) and this is what it does:
    Code:
    List<String> players = new ArrayList<String>();
    
    Hope I provided as much info as you guys need.

    ~ AdamDev
     
  2. Offline

    RcExtract

    Try to run the code with that error again. Then the location of the exception thrown should appear.

    Some suggestions:
    1. Use ChatColor enum instead of §
    2. Pass the plugin instance through constructor is better
    3. After returning true inside if, u don't need else if, simply if is ok
    4. After doing something when the BlackMarket.getInventory() is null, you don't need to them check if it is not null, cuz it must be not null when checked not null, so the second if is unnecessary.

    I think the problem is not related to core.players.
     
    Last edited: Oct 1, 2017
  3. Offline

    AdamDev

    @RcExtract
    1. I tried locating the error many times before I posted this, it never appeared.
    2. I know but it works perfectly fine in-game.
    3. When using lists, it can't be passed through a constructor. And I already have a constructor if you looked at the code better.
    4. I know that yet again I just put that to make sure it's not null and not true or false.
    5. Where do you see "BlackMarket.getInventory()"?

    I hope I gave you some help on the situation.
     
  4. Offline

    Caderape2

    @AdamDev
    Remove the = and it should be good.
    In an array, the last index it's always the size - 1

    The second statement will always be true. The point to use if else, it's if the condition in the 'if' is not respected, the 'else' will be executed. and vice versa
     
    Last edited: Oct 2, 2017
    ipodtouch0218 likes this.
  5. Offline

    AdamDev

  6. Offline

    Caderape2

    @AdamDev
    Why for each content of the inventory, you loop the list ? Since you always set to the slot i, the itemstack will be created again and again and erase the old one. So you will only see the player who is the last in the list.

    For the material, it's nothing hard.
     
Thread Status:
Not open for further replies.

Share This Page