Blankj/AndroidUtilCode · error · IllegalArgumentException

Unsupported object type: {}

Error message

Unsupported object type: {}

What it means

Thrown by CollectionUtils.get(object, index) when the object is not a Map, List, Object[], Iterator, Collection, or Enumeration, and the fallback Array.get(object, index) raises IllegalArgumentException (the object is not array-like). The library wraps that into an 'Unsupported object type' message naming the class. It indicates the caller passed a scalar or otherwise non-indexable object.

Source

Thrown at lib/utilcode/src/main/java/com/blankj/utilcode/util/CollectionUtils.java:749

        } else if (object instanceof Collection) {
            Iterator iterator = ((Collection) object).iterator();
            return get(iterator, index);
        } else if (object instanceof Enumeration) {
            Enumeration it = (Enumeration) object;
            while (it.hasMoreElements()) {
                index--;
                if (index == -1) {
                    return it.nextElement();
                } else {
                    it.nextElement();
                }
            }
            throw new IndexOutOfBoundsException("Entry does not exist: " + index);
        } else {
            try {
                return Array.get(object, index);
            } catch (IllegalArgumentException ex) {
                throw new IllegalArgumentException("Unsupported object type: " + object.getClass().getName());
            }
        }
    }

    /**
     * Gets the size of the collection/iterator specified.
     * <p>
     * This method can handles objects as follows
     * <ul>
     * <li>Collection - the collection size
     * <li>Map - the map size
     * <li>Array - the array size
     * <li>Iterator - the number of elements remaining in the iterator
     * <li>Enumeration - the number of elements remaining in the enumeration
     * </ul>
     *
     * @param object the object to get the size of
     * @return the size of the specified collection

View on GitHub (pinned to 7b4caf9e54)

Solutions

  1. Branch on the object's type before calling get; handle scalars separately.
  2. Normalize the input to a Collection/array (e.g., wrap a single scalar in a list) before indexing.
  3. Use CollectionUtils.size(object) safely or check instanceof Collection/List first.
  4. Tighten the API so only indexable types reach get().

Example fix

// before
Object v = CollectionUtils.get(jsonValue, 0); // String -> Unsupported object type

// after
List<?> items = (jsonValue instanceof Collection)
    ? new ArrayList<>((Collection<?>) jsonValue)
    : Collections.singletonList(jsonValue);
Object v = items.get(0);
Defensive patterns

Strategy: type-guard

Validate before calling

if (object instanceof Map || object instanceof List || object instanceof Collection
        || object instanceof Iterator || object instanceof Enumeration
        || (object != null && object.getClass().isArray())) {
    Object v = CollectionUtils.get(object, index);
} else {
    // scalar/non-indexable; handle separately
}

Type guard

static boolean isIndexable(Object o) {
    if (o == null) return false;
    return o instanceof Map || o instanceof List || o instanceof Collection
        || o instanceof Iterator || o instanceof Enumeration
        || o.getClass().isArray();
}

Try / catch

try {
    v = CollectionUtils.get(object, index);
} catch (IllegalArgumentException e) {
    v = null; // unsupported object type
}

Prevention

When it happens

Trigger: Calling get on a String, boxed primitive (Integer/Long), custom POJO, or any type that is neither a collection nor an array; passing an object whose runtime class changed from a collection to a scalar.

Common situations: Loosely-typed code paths where the object's type is unknown; deserializing JSON into a Map vs a scalar depending on payload; passing config values that may be a single object or a list.

Related errors


AI-assisted analysis of Blankj/AndroidUtilCode@7b4caf9e54 (2026-08-14). Data as JSON: /api/errors/7ab04d2bc1bf3be3. Report an issue: GitHub.