0

Here is my part of code, which throws exception:

List<Car> car1store = new ArrayList<>();

//adding any number of cars to car1store

Random rnd = new Random();
double randNumber = rnd.nextDouble();

if (randNumber < 0.25) {
    int attempt = rnd.nextInt(car1store.size()); 
    Car car = car1store.get(attempt);
}

I have already searched for why does this exception appear, but I seriously can't find how car1store.size() can be a negative number. The least number it can be is only a zero. Can you see my mistake?

enneenne
  • 209
  • 1
  • 8
  • First off, I would make car1store an ArrayList rathing than relying on polymorphism. After you change that could you please send the exact line the error occurs on? Thanks!!! – Patrick Oshea Apr 29 '20 at 19:34
  • Zero is not positive. It also isn't negative, but it's not positive. The argument for `nextInt()` is an exclusive upper bound, so it has to be greater than 0 - after all, it makes no sense to ask for "a number >= 0, but also < 0" – Green Cloak Guy Apr 29 '20 at 19:34
  • @PatrickOshea yes, just appeared again. Did several attempts and it's here again. – enneenne Apr 29 '20 at 19:37
  • 1
    `int attempt` also can be negative because of `rnd`. Refer [this](https://stackoverflow.com/questions/32101688/illegalargumentexception-bound-must-be-positive) – Kalana Apr 29 '20 at 19:38
  • This line: int attempt = rnd.nextInt(car1store.size()); – enneenne Apr 29 '20 at 19:39
  • @Kalana From the docs - _Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator's sequence._ How can it return a negative? – Harshal Parekh Apr 29 '20 at 19:42
  • Show us how you are adding a car to `car1store` – neildo Apr 29 '20 at 19:46
  • @enneenne - If one of the answers resolved your issue, you can help the community by marking it as accepted. An accepted answer helps future visitors use the solution confidently. Check https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work to learn how to do it. – Arvind Kumar Avinash May 14 '20 at 12:27

2 Answers2

1

Bound must be positive

The error message clearly states the issue here. The size of your ArrayList is 0. It is not positive.

The bigger problem here is that you are trying to .get() from an empty list.

You should be adding some elements to the list and then try to access it.

Harshal Parekh
  • 5,918
  • 4
  • 21
  • 43
1

You faced this problem because car1store.size() is 0 and rnd.nextInt requires a bound greater than 0. You can understand the problem with the following code:

import java.util.Random;

public class Main {
    public static void main(String[] args) {
        Random rand = new Random();
        System.out.println(rand.nextInt(0));
    }
}

Output:

Exception in thread "main" java.lang.IllegalArgumentException: bound must be positive
    at java.base/java.util.Random.nextInt(Random.java:388)
    at Main.main(Main.java:6)
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110