jwtk/jjwt · error · IllegalArgumentException

Collection must not be null

Error message

Collection must not be null

What it means

IllegalArgumentException thrown by io.jsonwebtoken.lang.Collections.mergeArrayIntoCollection when the target Collection argument is null. The source array may be null (it is tolerated), but the destination collection must exist.

Source

Thrown at api/src/main/java/io/jsonwebtoken/lang/Collections.java:287

    @SafeVarargs
    public static <T> Set<T> concat(Set<T> c, T... elements) {
        int size = Math.max(1, Collections.size(c) + io.jsonwebtoken.lang.Arrays.length(elements));
        Set<T> set = new LinkedHashSet<>(size);
        set.addAll(c);
        java.util.Collections.addAll(set, elements);
        return immutable(set);
    }

    /**
     * Merge the given array into the given Collection.
     *
     * @param array      the array to merge (may be <code>null</code>)
     * @param collection the target Collection to merge the array into
     */
    @SuppressWarnings("unchecked")
    public static void mergeArrayIntoCollection(Object array, Collection collection) {
        if (collection == null) {
            throw new IllegalArgumentException("Collection must not be null");
        }
        Object[] arr = Objects.toObjectArray(array);
        java.util.Collections.addAll(collection, arr);
    }

    /**
     * Merge the given Properties instance into the given Map,
     * copying all properties (key-value pairs) over.
     * <p>Uses <code>Properties.propertyNames()</code> to even catch
     * default properties linked into the original Properties instance.
     *
     * @param props the Properties instance to merge (may be <code>null</code>)
     * @param map   the target Map to merge the properties into
     */
    @SuppressWarnings("unchecked")
    public static void mergePropertiesIntoMap(Properties props, Map map) {
        if (map == null) {
            throw new IllegalArgumentException("Map must not be null");

View on GitHub (pinned to fb71496164)

Solutions

  1. Pass a non-null, mutable Collection (e.g. new ArrayList<>()) as the target
  2. Check the argument order — a null first argument (the array) is allowed, a null second is not
  3. Initialize the collection field before merging

Example fix

// before
Collections.mergeArrayIntoCollection(values, targetList); // targetList is null
// after
List<Object> targetList = new ArrayList<>();
Collections.mergeArrayIntoCollection(values, targetList);
Defensive patterns

Strategy: validation

Validate before calling

if (collection == null) {
    collection = new ArrayList<>();
}
Collections.mergeArrayIntoCollection(array, collection);

Try / catch

try {
    Collections.mergeArrayIntoCollection(array, collection);
} catch (IllegalArgumentException e) {
    throw new IllegalStateException("Target collection must be initialized before merge", e);
}

Prevention

When it happens

Trigger: Calling Collections.mergeArrayIntoCollection(array, collection) with a null collection — e.g. a field never initialized or a getter that returned null.

Common situations: Passing an uninitialized List field; a helper method that returns null when no collection is configured; copy-paste where the array and collection arguments were swapped.

Related errors


AI-assisted analysis of jwtk/jjwt@fb71496164 (2026-09-09). Data as JSON: /api/errors/2ffe87b0c865edac. Report an issue: GitHub.