IllegalArgumentException: Bound must be positive

让人想犯罪 __ 提交于 2019-11-27 07:51:17

问题


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]

回答1:


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())));
}



回答2:


As far as your stacktrace 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.



来源:https://stackoverflow.com/questions/32101688/illegalargumentexception-bound-must-be-positive

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!