jwtk/jjwt · error · IllegalArgumentException

Class method parameter cannot be null.

Error message

Class method parameter cannot be null.

What it means

IllegalArgumentException thrown by io.jsonwebtoken.lang.Classes.newInstance(Class) when the Class argument is null. It is a fail-fast precondition check before attempting reflection-based instantiation.

Source

Thrown at api/src/main/java/io/jsonwebtoken/lang/Classes.java:216

     * @param <T>  the type of the object created
     * @return the newly created object
     */
    @SuppressWarnings("unchecked")
    public static <T> T newInstance(String fqcn, Object... args) {
        return (T) newInstance(forName(fqcn), args);
    }

    /**
     * Creates a new instance of the specified {@code clazz} via {@code clazz.newInstance()}.
     *
     * @param clazz the class to invoke
     * @param <T>   the type of the object created
     * @return the newly created object
     */
    public static <T> T newInstance(Class<T> clazz) {
        if (clazz == null) {
            String msg = "Class method parameter cannot be null.";
            throw new IllegalArgumentException(msg);
        }
        try {
            return clazz.newInstance();
        } catch (Exception e) {
            throw new InstantiationException("Unable to instantiate class [" + clazz.getName() + "]", e);
        }
    }

    /**
     * Returns a new instance of the specified {@code clazz}, invoking the associated constructor with the specified
     * {@code args} arguments.
     *
     * @param clazz the class to invoke
     * @param args  the arguments matching an associated class constructor
     * @param <T>   the type of the created object
     * @return the newly created object
     */
    public static <T> T newInstance(Class<T> clazz, Object... args) {

View on GitHub (pinned to fb71496164)

Solutions

  1. Check the Class argument for null before calling Classes.newInstance
  2. Fix the upstream lookup (class name config key, Classes.forName call) that produced null
  3. If a null is expected, guard with a default implementation class

Example fix

// before
Class<MyCodec> c = lookupCodecClass(); // may be null
MyCodec codec = Classes.newInstance(c);
// after
Class<MyCodec> c = lookupCodecClass();
if (c == null) {
    c = MyCodec.class; // or throw a clearer config error
}
MyCodec codec = Classes.newInstance(c);
Defensive patterns

Strategy: type-guard

Validate before calling

if (clazz == null) {
    clazz = DefaultImplClass.class; // or fail with a clear config error
}

Type guard

Class<?> resolveClass(Class<?> c) {
    return (c != null) ? c : DefaultImplClass.class;
}

Try / catch

try {
    return Classes.newInstance(clazz);
} catch (IllegalArgumentException e) {
    throw new IllegalStateException("Implementation class not configured; cannot instantiate null class", e);
}

Prevention

When it happens

Trigger: Passing a null Class<T> to Classes.newInstance(clazz), commonly because an earlier lookup (e.g. Classes.forName, a config-driven class name, or a map lookup) returned or was assigned null.

Common situations: Configuration property naming a class is missing so a lookup yields null that is then handed to newInstance; calling getImplementationClass-style helpers without checking for null; test code constructing instances from a null constant.

Related errors


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