Java.util.random - "Linked must be positive"

I am getting this error:

java.lang.illegalException:
bound must be posotive

      

in the next line of code:

Random random = new Random();
Item randomItem;
randomItem = new Item("NAME_HERE", "DESCRIPTION_HERE", 0000);
int randomNumber = random.nextInt(randomItem.randomNames.size());   *** ERROR HERE?!
randomItem.name = randomItem.randomNames.get(randomNumber);
addItem(randomItem);

      

This only happens during the SECOND time when I call the above method. It looks like (if I try to print the size of the randomNames arraylist then it actually reaches 0. But that shouldn't ... Here is the random element in which the randomNames charismatic is created. Nothing else but the above code has an impact.

public ArrayList<String> randomNames;
randomNames.add("Key");
randomNames.add("Rock");
randomNames.add("Balloon");
randomNames.add("Boot");
randomNames.add("Knife");
randomNames.add("Pencil");

      

+3


source to share


2 answers


Your randomItems array size is 0. Make sure



  • randomNames is not declared static
  • Item constructor fills random Array items
+1


source


Have you instantiated the list?

randomNames = new ArrayList<String>();

      



then you can add all the items you want.

0


source







All Articles