0
\$\begingroup\$

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) + ", ";
            }
        }
\$\endgroup\$
4
\$\begingroup\$

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) + ", ";
        }
    }
|improve this answer|||||
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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