Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm getting index out of bounds errors in my Bukkit plugin, and it's really beginning to piss me off...

I for the life of me can't figure this issue out!

Caused by: java.lang.IndexOutOfBoundsException: Index: 1, Size: 1

This is where I believe the code to be erroring...

for(int i = 0; i <= staffOnline.size(); i++) {
            if(i == staffOnline.size()) {
                staffList = staffList + staffOnline.get(i);
            } else {
                staffList = staffList + staffOnline.get(i) + ", ";
            }
        }
share|improve this question

1 Answer 1

up vote 4 down vote accepted

The objects in collections in Java are usually indexed starting with zero. Also, the method .size() returns the numbers of elements inside the collection.

So, if the size is 1, you can only access the element 0. Not 0 and 1 like you're doing, because the element 1 do not exist.

Try this:

for(int i = 0; i < staffOnline.size(); i++) { // changed here
        if(i == staffOnline.size() - 1) { // and here :)
            staffList = staffList + staffOnline.get(i);
        } else {
            staffList = staffList + staffOnline.get(i) + ", ";
        }
    }
share|improve this answer
    
Thanks, you fixed a few bugs for me their :D –  Robby Duke Nov 3 '12 at 14:46

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.