Solved index out of bounds help

Discussion in 'Plugin Development' started by 1Camer0471, Aug 30, 2015.

Thread Status:
Not open for further replies.
  1. Offline

    1Camer0471

    Code:Java
    1.  
    2. List<String> lore = item.getItemMeta().getLore();
    3. for (int index = 0; index <= lore.size(); index++) {
    4. System.out.println(index + ", " + lore.get(index));
    5. if (lore.get(index).contains("Merchant")
    6. || lore.get(index).contains("Price")) {
    7. lore.remove(index);
    8. }
    9. }
    10.  


    The exception is on line 4, here is the error

    Caused by: java.lang.IndexOutOfBoundsException: Index: 2, Size: 2
     
  2. Offline

    Oxyorum

    @1Camer0471

    That for loop is running for all elements in the List object lore, plus one. Since indexes start at 0, the last index value will be list.size() -1, which is "out of bounds" and why your code is throwing an exception.

    Tl;Dr: Change index <= lore.size() to index < lore.size().
     
  3. Offline

    teej107

    @1Camer0471 You're going outside of the the List size bounds.
     
  4. Offline

    1Camer0471

    what do you mean? there are 3 lines of lore: 0, 1, 2 it's reading lines 0 and 1, but then stopping at line 2?
     
  5. Offline

    teej107

    Shortninja66 likes this.
  6. Offline

    Oxyorum

    @1Camer0471 Whenever an item in your lore matches the criteria you specified in your if statement, it will get removed from the list. I assume one of the items in the lore list does match with the criteria, and so it is removed from the list, leaving you with two indexes (0 and 1). That is why the number being given in the exception is 2, because the size is 2 and the last possible index is the size minus 1, so its 1.
     
    Last edited: Aug 30, 2015
  7. Offline

    Shortninja66

    *cough* less than or equals *cough*
     
  8. Offline

    sionzee

    Why u here using for(i) loop? U don't need it. Just use Foreach.
    Code:
    for(String line : lore) {
        if (line.contains("Merchant") || line.contains("Price")) {
            lore.remove(line);
        }
    }
     
  9. Offline

    teej107

    @sionzee Are you trying to throw errors on purpose?!?!
     
  10. Offline

    sionzee

    @teej107 What? What about u talking? :confused: That what I said is correct. No errors, no throwing...
     
  11. Try out your "working" code yourself. Have fun with your ConcurrentModificationException :p
     
  12. Offline

    teej107

    That piece of code can throw a ConcurrentModificationException.
     
  13. Offline

    sionzee

    You can clone list and then replace him. (Alternate solution)
    Ok, I don't know why getLore is unmodifitable list (they can @Override add, remove method to minecraft usage)
    But ok. Probably all have own reason. It's a Bukkit core....

    And I can't know if item is ItemStack and getLore is from ItemMeta#getLore...
    He can use it as own classes.

    BTW:
    replace
    for (int index = 0; index <= lore.size(); index++) {
    to
    for (int index = 0; index < lore.size(); index++) {
    Because it start from index zero.
     
  14. Offline

    1Camer0471

    lmao a mini fire war went down while I was eating supper :p
    @teej107 @sionzee @FisheyLP

    I fixed it, thanks for your help! <3
     
  15. Offline

    Zombie_Striker

Thread Status:
Not open for further replies.

Share This Page