您现在的位置是:网站首页 > 代码编程 > JAVA开发JAVA开发

【原】Java生成随机数报错“java.lang.IllegalArgumentException: bound must be positive”

不忘初心 不忘初心 2019-07-21 围观() 评论() 点赞() JAVA开发

简介:之前写了一个java生成随机数的公共方法,很简陋,但是这么久了也没出过什么问题,今天利用junit做单元测试时,却报了一个错:“java.lang.IllegalArgumentException: bound must be positive”。

之前写了一个java生成随机数的公共方法,很简陋,但是这么久了也没出过什么问题,今天利用junit做单元测试时,却报了一个错:“java.lang.IllegalArgumentException: bound must be positive”。

详细错误信息如下:

java.lang.IllegalArgumentException: bound must be positive

	at java.util.Random.nextInt(Random.java:388)
	at com.wolffy.util.IntegerUtil.random(IntegerUtil.java:36)
	at com.wolffy.jwcz.service.impl.ArticleServiceImpl.awareAndFilter(ArticleServiceImpl.java:880)
	at com.wolffy.jwcz.service.impl.ArticleServiceImpl.insert(ArticleServiceImpl.java:145)
	at com.wolffy.jwcz.service.impl.ArticleServiceImpl.insert(ArticleServiceImpl.java:57)
	at com.wolffy.jwcz.service.impl.ArticleServiceImpl$FastClassBySpringCGLIB$9dca5f66.invoke(<generated>)
	at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
	at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:721)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
	at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
	at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:282)
	at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
	at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
	at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:656)
	at com.wolffy.jwcz.service.impl.ArticleServiceImpl$EnhancerBySpringCGLIB$215d7b18.insert(<generated>)
	at SpiderTest.test(SpiderTest.java:50)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
	at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
	at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:252)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
	at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
	at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
	at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
	at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
	at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

代码如下:

public static Integer random(int bound) {
    // return new Random().nextInt(bound);
    return ThreadLocalRandom.current().nextInt(bound);
}

debug排查了一番,发现是自己的代码不严谨,在传递bound进来的时候出现了0的情况。

public static void main(String[] args) {
    System.out.println(random(0));
}

上诉代码,结果就是这个错误:

Java生成随机数报错“java.lang.IllegalArgumentException: bound must be positive”

解决,改进:

public static Integer random(int bound) {
    if (bound == 0) {
        return 0;
    }
    // return new Random().nextInt(bound);
    return ThreadLocalRandom.current().nextInt(bound);
}

java随机数

看完文章,有任何疑问,请加入群聊一起交流!!!

很赞哦! ()

文章评论

  • 请先说点什么
    人参与,条评论

请使用电脑浏览器访问本页面,使用手机浏览器访问本页面会导致下载文件异常!!!

雨落无影

关注上方公众号,回复关键字【下载】获取下载码

用完即删,每次下载需重新获取下载码

若出现下载不了的情况,请及时联系站长进行解决

站点信息

  • 网站程序:spring + freemarker
  • 主题模板:《今夕何夕》
  • 文章统计:篇文章
  • 标签管理标签云
  • 微信公众号:扫描二维码,关注我们