alibaba/nacos · warning · IllegalArgumentException

Expected an array of elements (or empty array) but received

Error message

Expected an array of elements (or empty array) but received a null.

What it means

Thrown by CollectionUtils.list(T... elements) when the varargs array itself is null. In normal call sites the compiler synthesizes a non-null array, so this only triggers when the method is called with an explicitly-passed null array (e.g., CollectionUtils.list((Object[]) null)) or via reflective/intermediate null propagation.

Source

Thrown at common/src/main/java/com/alibaba/nacos/common/utils/CollectionUtils.java:276

     */
    public static <T> T getOrDefault(Object obj, int index, T defaultValue) {
        try {
            return (T) get(obj, index);
        } catch (IndexOutOfBoundsException e) {
            return defaultValue;
        }
    }
    
    /**
     * return an arraylist containing all input parameters.
     *
     * @param elements element array
     * @return arraylist containing all input parameters
     * @author zzq
     */
    public static <T> List<T> list(T... elements) {
        if (elements == null) {
            throw new IllegalArgumentException(
                "Expected an array of elements (or empty array) but received a null.");
        }
        ArrayList<T> list = new ArrayList<>(elements.length);
        Collections.addAll(list, elements);
        return list;
    }
    
    /**
     * Return a set containing all input parameters.
     *
     * @param elements elements element array
     * @return set containing all input parameters
     */
    public static <T> Set<T> set(T... elements) {
        if (elements == null) {
            throw new IllegalArgumentException(
                "Expected an array of elements (or empty array) but received a null.");
        } else {

View on GitHub (pinned to 9b989acdf1)

Solutions

  1. Guard the caller: if (arr != null) CollectionUtils.list(arr) else Collections.emptyList().
  2. Pass zero explicit arguments (CollectionUtils.list()) to get an empty list rather than passing null.
  3. Avoid casting null to a typed array just to call the varargs method.

Example fix

// before
List<T> l = CollectionUtils.list((T[]) maybeNullArray);

// after
List<T> l = maybeNullArray == null ? new ArrayList<>() : CollectionUtils.list(maybeNullArray);
Defensive patterns

Strategy: validation

Validate before calling

List<T> safeList(T[] arr) {
    return arr == null ? new ArrayList<>() : CollectionUtils.list(arr);
}

Prevention

When it happens

Trigger: Calling list((T[]) null) explicitly, or passing a typed array variable that is null where the compiler does not box it into a new array.

Common situations: Passing a nullable array variable directly: CollectionUtils.list(nullableArray); reflective invocation that supplies null for the varargs parameter.

Related errors


AI-assisted analysis of alibaba/nacos@9b989acdf1 (2026-08-14). Data as JSON: /api/errors/4074af82addc53c5. Report an issue: GitHub.