13

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]
spongebob
  • 8,370
  • 15
  • 50
  • 83
Guus Huizen
  • 165
  • 1
  • 2
  • 9
  • 3
    Well ... obviously you have an empty items list, so that `items.size()` evaluates to 0. And this is not allowed as a bound for `Random.nextInt(int)`. – Seelenvirtuose Aug 19 '15 at 17:15
  • 2
    @Seelenvirtuose It may not be obvious to everyone! But this is the case, `getAllItems()` is returning an empty list of items. – Keenan Thompson Aug 19 '15 at 17:19
  • Ok, so at the 'getAllItems()' method, i changed '"chestitems." + level' to '"chestitems.1"'. Now the system does work. I still don't understand it although I know this method is returning null. – Guus Huizen Aug 19 '15 at 17:28
  • Ok, so I was being dumb again. I rechecked the list and found out that something was 0. Changed it and it worked like a charm. Thanks for all your help! – Guus Huizen Aug 19 '15 at 17:32

2 Answers2

24

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())));
}
Todd
  • 30,472
  • 11
  • 81
  • 89
6

As your stack trace says,

java.lang.IllegalArgumentException: bound must be positive at java.util.Random.nextInt(Unknown Source) ~[?:1.8.0_51]

The argument to nextInt() needs to be a positive integer. You will need to find out where you're passing a non-positive input to that method.

Kirby
  • 15,127
  • 10
  • 89
  • 104
Swapnil
  • 8,201
  • 4
  • 38
  • 57