IllegalArgumentException: Bound must be positive

Guus Huizen picture Guus Huizen · Aug 19, 2015 · Viewed 52.3k times · Source

I get an error saying that my bound must be positive. Here is the line I get it on:

inv.setItem(i, items.get(r.nextInt(items.size())));

As far as I know, it comes from the part where I request a random integer from the list of items. This is how I defined the list:

List<ItemStack> items = getAllItems(level);

Where the getAllItems() method looks like:

public List<ItemStack> getAllItems(int level) {
    List<ItemStack> items = new ArrayList<ItemStack>();
    for (String item : settings.getChests().getStringList("chestitems." + level)) {
        ItemStack toAdd = parseItem(item);
        items.add(toAdd);
    }
    return items;
}

I get this stacktrace:

[19:03:53 ERROR]: Error occurred while enabling KitPvP v0.5 (Is it up to date?)
java.lang.IllegalArgumentException: bound must be positive
        at java.util.Random.nextInt(Unknown Source) ~[?:1.8.0_51]
        at me.iamguus.gamegetsiepunt.kitpvp.chests.ChestsUtil.randomlyFillInv(ChestsUtil.java:101) ~[?:?]
        at me.iamguus.gamegetsiepunt.kitpvp.Main.onEnable(Main.java:40) ~[?:?]
        at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:321) ~[spigot.jar:git-Spigot-5818108-a486600]
        at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:335) [spigot.jar:git-Spigot-5818108-a486600]
        at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:405) [spigot.jar:git-Spigot-5818108-a486600]
        at org.bukkit.craftbukkit.v1_8_R3.CraftServer.loadPlugin(CraftServer.java:356) [spigot.jar:git-Spigot-5818108-a486600]
        at org.bukkit.craftbukkit.v1_8_R3.CraftServer.enablePlugins(CraftServer.java:316) [spigot.jar:git-Spigot-5818108-a486600]
        at org.bukkit.craftbukkit.v1_8_R3.CraftServer.reload(CraftServer.java:746) [spigot.jar:git-Spigot-5818108-a486600]
        at org.bukkit.Bukkit.reload(Bukkit.java:534) [spigot.jar:git-Spigot-5818108-a486600]
        at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:25) [spigot.jar:git-Spigot-5818108-a486600]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) [spigot.jar:git-Spigot-5818108-a486600]
        at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:646) [spigot.jar:git-Spigot-5818108-a486600]
        at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchServerCommand(CraftServer.java:632) [spigot.jar:git-Spigot-5818108-a486600]
        at net.minecraft.server.v1_8_R3.DedicatedServer.aO(DedicatedServer.java:405) [spigot.jar:git-Spigot-5818108-a486600]
        at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:369) [spigot.jar:git-Spigot-5818108-a486600]
        at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:657) [spigot.jar:git-Spigot-5818108-a486600]
        at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:560) [spigot.jar:git-Spigot-5818108-a486600]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_51]

Answer

Todd picture Todd · Aug 19, 2015

The issue is that you are calling Random.nextInt() with a zero and it doesn't like that. That is happening because the List from getAllItems() is empty. I would prevent this situation by checking that the list has items before performing your logic:

List<ItemStack> items = getAllItems(level);
if(!items.isEmpty()) {
    inv.setItem(i, items.get(r.nextInt(items.size())));
}