jwtk/jjwt · error · IllegalArgumentException

Source is not an array:

Error message

Source is not an array: 

What it means

Thrown by Objects.toObjectArray when the given source object is neither an Object[] nor null and is not a primitive/other array type, so it cannot be converted to an Object[]. It is a defensive guard in array-conversion helpers.

Source

Thrown at api/src/main/java/io/jsonwebtoken/lang/Objects.java:263

    /**
     * Convert the given array (which may be a primitive array) to an
     * object array (if necessary of primitive wrapper objects).
     * <p>A <code>null</code> source value will be converted to an
     * empty Object array.
     *
     * @param source the (potentially primitive) array
     * @return the corresponding object array (never <code>null</code>)
     * @throws IllegalArgumentException if the parameter is not an array
     */
    public static Object[] toObjectArray(Object source) {
        if (source instanceof Object[]) {
            return (Object[]) source;
        }
        if (source == null) {
            return new Object[0];
        }
        if (!source.getClass().isArray()) {
            throw new IllegalArgumentException("Source is not an array: " + source);
        }
        int length = Array.getLength(source);
        if (length == 0) {
            return new Object[0];
        }
        Class wrapperType = Array.get(source, 0).getClass();
        Object[] newArray = (Object[]) Array.newInstance(wrapperType, length);
        for (int i = 0; i < length; i++) {
            newArray[i] = Array.get(source, i);
        }
        return newArray;
    }


    //---------------------------------------------------------------------
    // Convenience methods for content-based equality/hash-code handling
    //---------------------------------------------------------------------

View on GitHub (pinned to fb71496164)

Solutions

  1. Pass an actual array (e.g. new Object[]{...}) instead of a List or other object
  2. Convert collections first: list.toArray(new Object[0])
  3. If the input may be either, branch on source.getClass().isArray() before calling
  4. Catch IllegalArgumentException if input is untrusted and handle the non-array case

Example fix

// before
Object[] arr = Objects.toObjectArray(myList); // IllegalArgumentException
// after
Object[] arr = myList.toArray(new Object[0]);
Defensive patterns

Strategy: type-guard

Validate before calling

if (source != null && !source.getClass().isArray()) throw new IllegalArgumentException("Expected an array, got: " + source.getClass());

Type guard

static boolean isObjectArray(Object o) {
    return o == null || o instanceof Object[] || (o != null && o.getClass().isArray());
}

Try / catch

try {
    Object[] arr = Objects.toObjectArray(source);
} catch (IllegalArgumentException e) {
    Object[] arr = new Object[]{ source }; // treat as single-element
}

Prevention

When it happens

Trigger: Passing a non-array object (e.g. a List, String, or arbitrary bean) to Objects.toObjectArray; the method only accepts arrays or null.

Common situations: Refactoring code that changed a varargs/array parameter to a plain object; accidentally passing a collection instead of an array; reflection-based code mis-detecting the argument type.

Understand the failure class

Background: "is not a compatible type" / "cannot merge" errors: when a value's type doesn't match what the library requires — this error's family across 65 libraries.

Related errors


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