Blankj/AndroidUtilCode · error · IndexOutOfBoundsException
Index cannot be negative: {}
Error message
Index cannot be negative: {} What it means
Thrown by CollectionUtils.get(object, index) when index is negative. The method supports Map, List, Object[], Iterator, Collection, and Enumeration, and rejects any negative index up front (before dispatching on type) since none of those structures has a meaningful negative position.
Source
Thrown at lib/utilcode/src/main/java/com/blankj/utilcode/util/CollectionUtils.java:710
* <li> Collection -- the value returned is the <code>index</code>-th object
* returned by the collection's default iterator, if there is such an element.</li>
* <li> Iterator or Enumeration -- the value returned is the
* <code>index</code>-th object in the Iterator/Enumeration, if there
* is such an element. The Iterator/Enumeration is advanced to
* <code>index</code> (or to the end, if <code>index</code> exceeds the
* number of entries) as a side effect of this method.</li>
* </ul>
*
* @param object the object to get a value from
* @param index the index to get
* @return the object at the specified index
* @throws IndexOutOfBoundsException if the index is invalid
* @throws IllegalArgumentException if the object type is invalid
*/
public static Object get(Object object, int index) {
if (object == null) return null;
if (index < 0) {
throw new IndexOutOfBoundsException("Index cannot be negative: " + index);
}
if (object instanceof Map) {
Map map = (Map) object;
Iterator iterator = map.entrySet().iterator();
return get(iterator, index);
} else if (object instanceof List) {
return ((List) object).get(index);
} else if (object instanceof Object[]) {
return ((Object[]) object)[index];
} else if (object instanceof Iterator) {
Iterator it = (Iterator) object;
while (it.hasNext()) {
index--;
if (index == -1) {
return it.next();
} else {
it.next();
}View on GitHub (pinned to 7b4caf9e54)
Solutions
- Check index >= 0 before calling get; treat negative indices as 'no selection'.
- When using indexOf results, guard for -1 / INDEX_NOT_FOUND first.
- Clamp user-supplied indices to a non-negative value.
- Switch to a Map lookup when keys are not positional.
Example fix
// before Object v = CollectionUtils.get(list, list.indexOf(target)); // -1 when absent // after int i = list.indexOf(target); Object v = (i >= 0) ? CollectionUtils.get(list, i) : null;
Defensive patterns
Strategy: validation
Validate before calling
if (index >= 0) {
Object v = CollectionUtils.get(object, index);
} else {
// negative index is never valid; treat as absent
} Try / catch
try {
v = CollectionUtils.get(object, index);
} catch (IndexOutOfBoundsException e) {
v = null; // negative index -> no value
} Prevention
- Always check index >= 0 before calling get.
- Guard indexOf()/List.indexOf() results for -1 before reuse as an index.
- Clamp parsed/external indices to a non-negative minimum.
When it happens
Trigger: Calling CollectionUtils.get(obj, index) with index < 0; passing a value returned from a failed indexOf (e.g., -1 meaning 'not found') directly as the index.
Common situations: Using the result of indexOf()/List.indexOf() (which returns -1 on miss) as the index without checking; underflow from subtracting offsets; parsing a negative number from external input as an index.
Related errors
- Entry does not exist: {}
- Unsupported object type: {}
- Index: {}, array1 Length: {}
- Index: {}, Length: 0
- Index: {}, Length: {}
AI-assisted analysis of Blankj/AndroidUtilCode@7b4caf9e54 (2026-08-14).
Data as JSON: /api/errors/8112870d775e3e4b.
Report an issue: GitHub.