简体   繁体   English

在Google Play的当机报告中获取Random.nextint的IllegalArgumentException

[英]Getting a IllegalArgumentException for Random.nextint in Google Play's crash report

I was looking at my crash report and it says the following report 我正在查看崩溃报告,并显示以下报告

java.lang.IllegalArgumentException in java.util.Random.nextInt java.util.Random.nextInt中的java.lang.IllegalArgumentException

I do not understand this, nextint takes only a int of the max random number to return, and java compiler will not allow you to put in a different type. 我不明白这一点,nextint仅接受最大随机数的int返回,并且java编译器不允许您放入其他类型。

How could this exception be going off????? 这种异常怎么可能发生????

仅当给定的值是非正数时,Nextint才会引发IllegalArgumentException。

If in doubt, you can always grep the JDK. 如有疑问,您可以随时grep JDK。 In this instance, you can look up Random.nextInt here . 在这种情况下,您可以在此处查找Random.nextInt。 Looking at the source code, IllegalArgumentException is thrown if and only if the given integer is not positive. 查看源代码,当且仅当给定的整数不是正数时,才会抛出IllegalArgumentException

For the sake of completeness, the relevant code reads: 为了完整起见,相关代码如下:

public int nextInt(int n) {
   if (n <= 0)
     throw new IllegalArgumentException("n must be positive");

   if ((n & -n) == n)  // i.e., n is a power of 2
     return (int)((n * (long)next(31)) >> 31);

   int bits, val;
   do {
       bits = next(31);
       val = bits % n;
    while (bits - val + (n-1) < 0);
   return val;
 }}

Edit: The OP asked a question in the comments, 编辑:OP在评论中问了一个问题,

could you explain "grep the JDK"? 你能解释一下“ grep JDK”吗?

Here I mean a search on grepcode.com for "Random.nextInt". 在这里,我的意思是在grepcode.com上搜索“ Random.nextInt”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM