[Fixed] IllegalArgumentException: Bound must be positive

In this post, we will see how to fix IllegalArgumentException: Bound must be positive in java.

You will generally get this exception while generating random numbers using Random.nextInt() method. If you pass bound as 0 or less than 0, then you will get the exception.

Let’s understand with the help of example:

When you run the code, you will get following exception.

As we have passed bound as 0 to nextInt(), we got above exception.

Here is source code Random’s nextInt() method.

As you can see, if bound is less than or equals to 0, we will get this exception.

To resolve the issue, pass positive number to nextInt() method.

Here is code to generate random number between 1 to 5.

When you run the code, you will get following exception.

As you can see, we have passed bound as 5 to nextInt() method and it worked fine now.

That’s all about how to fix IllegalArgumentException: Bound must be positive.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *