{"record":{"id":"4074af82addc53c5","repo":"alibaba/nacos","slug":"expected-an-array-of-elements-or-empty-array-but","errorCode":null,"errorMessage":"Expected an array of elements (or empty array) but received a null.","messagePattern":"Expected an array of elements \\(or empty array\\) but received a null\\.","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"warning","filePath":"common/src/main/java/com/alibaba/nacos/common/utils/CollectionUtils.java","lineNumber":276,"sourceCode":"     */\n    public static <T> T getOrDefault(Object obj, int index, T defaultValue) {\n        try {\n            return (T) get(obj, index);\n        } catch (IndexOutOfBoundsException e) {\n            return defaultValue;\n        }\n    }\n    \n    /**\n     * return an arraylist containing all input parameters.\n     *\n     * @param elements element array\n     * @return arraylist containing all input parameters\n     * @author zzq\n     */\n    public static <T> List<T> list(T... elements) {\n        if (elements == null) {\n            throw new IllegalArgumentException(\n                \"Expected an array of elements (or empty array) but received a null.\");\n        }\n        ArrayList<T> list = new ArrayList<>(elements.length);\n        Collections.addAll(list, elements);\n        return list;\n    }\n    \n    /**\n     * Return a set containing all input parameters.\n     *\n     * @param elements elements element array\n     * @return set containing all input parameters\n     */\n    public static <T> Set<T> set(T... elements) {\n        if (elements == null) {\n            throw new IllegalArgumentException(\n                \"Expected an array of elements (or empty array) but received a null.\");\n        } else {","sourceCodeStart":258,"sourceCodeEnd":294,"githubUrl":"https://github.com/alibaba/nacos/blob/9b989acdf181d00898f2e8839257bb2b2a3cefe3/common/src/main/java/com/alibaba/nacos/common/utils/CollectionUtils.java#L258-L294","documentation":"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.","triggerScenarios":"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.","commonSituations":"Passing a nullable array variable directly: CollectionUtils.list(nullableArray); reflective invocation that supplies null for the varargs parameter.","solutions":["Guard the caller: if (arr != null) CollectionUtils.list(arr) else Collections.emptyList().","Pass zero explicit arguments (CollectionUtils.list()) to get an empty list rather than passing null.","Avoid casting null to a typed array just to call the varargs method."],"exampleFix":"// before\nList<T> l = CollectionUtils.list((T[]) maybeNullArray);\n\n// after\nList<T> l = maybeNullArray == null ? new ArrayList<>() : CollectionUtils.list(maybeNullArray);","handlingStrategy":"validation","validationCode":"List<T> safeList(T[] arr) {\n    return arr == null ? new ArrayList<>() : CollectionUtils.list(arr);\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Call list() with explicit elements or an empty call, never a null array.","Null-check any array variable before forwarding to the varargs method."],"tags":["collections","varargs","null-check","utility"],"backgroundTag":null,"analyzedSha":"9b989acdf181d00898f2e8839257bb2b2a3cefe3","analyzedAt":"2026-08-14T07:17:31.569Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}