didi/DoKit · error · IndexOutOfBoundsException
Index cannot be negative: " + index
Error message
Index cannot be negative: " + index
What it means
CollectionUtils.get(object, index) rejects any negative index immediately with IndexOutOfBoundsException. The method supports Map, List, Object[], Iterator, Collection, Enumeration and reflective arrays, and the negative check runs before any dispatch, so it applies to all of them. This is a strict argument-validation failure, independent of the container's contents.
Source
Thrown at Android/dokit-util/src/main/java/com/didichuxing/doraemonkit/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 626827cddb)
Solutions
- Check index >= 0 before calling, returning null or a default when it is not.
- Never feed an indexOf()/find() result straight in; check for -1 (or use ArrayUtils.INDEX_NOT_FOUND) first.
- Clamp the index: Math.max(0, Math.min(index, size - 1)) when a boundary element is acceptable.
Example fix
// before
Object prev = CollectionUtils.get(items, currentPosition - 1); // throws at position 0
// after
Object prev = currentPosition > 0
? CollectionUtils.get(items, currentPosition - 1)
: null; Defensive patterns
Strategy: validation
Validate before calling
if (index < 0) return null; return CollectionUtils.get(object, index);
Prevention
- Never pass indexOf() results without checking for -1.
- Guard i-1 / i+1 computations at loop boundaries.
- Unit-test boundary indices 0 and size-1 for any indexed helper.
When it happens
Trigger: Passing index < 0, e.g. CollectionUtils.get(list, position - 1) when position == 0; computing an index with Math.round or integer division that can go negative on empty input.
Common situations: RecyclerView/ListView adapter code translating an item position to a collection index; 'previous element' lookups (i - 1) at i == 0; sentinel -1 from indexOf being passed through unchecked.
Related errors
- Entry does not exist: " + index
- Transformation list must not be null.
- maxStoredHeapDumps must be at least 1
- Index: " + index + ", array1 Length: 0
- Index: " + index + ", array1 Length: " + len1
AI-assisted analysis of didi/DoKit@626827cddb (2026-08-14).
Data as JSON: /api/errors/9ff5bba24d97f3bf.
Report an issue: GitHub.