Java IllegalArgumentException: Bound 必须为正

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32101688/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 12:02:52  来源:igfitidea点击:

IllegalArgumentException: Bound must be positive

javarandombukkitillegalargumentexception

提问by Guus Huizen

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:

getAllItems()方法如下所示:

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]

回答by Swapnil

As far as your stacktrace says,

至于你的堆栈跟踪说,

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

java.lang.IllegalArgumentException:在 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.

nextInt 的参数需要是一个正整数。您需要找出将非正输入传递给该方法的位置。

回答by Todd

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

问题是你Random.nextInt()用零打电话,它不喜欢那样。这是因为ListfromgetAllItems()是空的。我会通过在执行逻辑之前检查列表中是否包含项目来防止这种情况:

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