1.12.2 Error: bound must be positive

Discussion in 'Spigot Plugin Development' started by Giorgino, Apr 19, 2020.

  1. Hi everyone, does anyone know how I can solve this problem? In practice when I click with the right click it should add a random item to me, it's just that when I click with the right click the problem comes out.
    Code (Text):
    Caused by: java.lang.IllegalArgumentException: bound must be positive
            at java.util.Random.nextInt(Unknown Source) ~[?:1.8.0_241]
            at me.test.utils.Loot(Utils.java:69) ~[?:?]
    Code (Java):
    Line 69 of Utils:
    int index = new Random().nextInt(Items.size());
     
  2. The error is "Bound must be positive" Whatever "Items.size()" isn't positive.

    EDIT:
    Give us a little more than one line of code? How can we see the context if we only have one line.
     
  3. Your items collection is probably empty. Try something like
    Code (Text):
    if(!Items.isEmpty()) {
        int index = new Random().nextInt(Items.size());
    }
     
  4. Code (Java):
                List<String> Items = Main.getInstance().getConfig().getStringList("Test." + test2 + "." + "TestItems");
                int index = new Random().nextInt(Items.size());
                String items = Items.get(index);
                ItemStack newItem = new ItemStack(Material.getMaterial(items.toUpperCase()));
                p.getInventory().addItem(new ItemStack[] {newItem});
     
  5. I don't believe 0 is a positive number (Edit: It's not with nextint according to the javadocs), so it probably is throwing the exception for that reason. As cPlays and Arjan said, either use an if statement or provide us with more context if you need it to be handled differently.

    Edit: He posted as I did :p

    https://docs.oracle.com/javase/8/docs/api/java/util/Random.html#nextInt-int-

    Read the exception nextint() throws and print out the size of your list :) (Hint: It must be greater than 0)