{"record":{"id":"65e25105280e9936","repo":"jwtk/jjwt","slug":"class-method-parameter-cannot-be-null","errorCode":null,"errorMessage":"Class method parameter cannot be null.","messagePattern":"Class method parameter cannot be null\\.","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"api/src/main/java/io/jsonwebtoken/lang/Classes.java","lineNumber":216,"sourceCode":"     * @param <T>  the type of the object created\n     * @return the newly created object\n     */\n    @SuppressWarnings(\"unchecked\")\n    public static <T> T newInstance(String fqcn, Object... args) {\n        return (T) newInstance(forName(fqcn), args);\n    }\n\n    /**\n     * Creates a new instance of the specified {@code clazz} via {@code clazz.newInstance()}.\n     *\n     * @param clazz the class to invoke\n     * @param <T>   the type of the object created\n     * @return the newly created object\n     */\n    public static <T> T newInstance(Class<T> clazz) {\n        if (clazz == null) {\n            String msg = \"Class method parameter cannot be null.\";\n            throw new IllegalArgumentException(msg);\n        }\n        try {\n            return clazz.newInstance();\n        } catch (Exception e) {\n            throw new InstantiationException(\"Unable to instantiate class [\" + clazz.getName() + \"]\", e);\n        }\n    }\n\n    /**\n     * Returns a new instance of the specified {@code clazz}, invoking the associated constructor with the specified\n     * {@code args} arguments.\n     *\n     * @param clazz the class to invoke\n     * @param args  the arguments matching an associated class constructor\n     * @param <T>   the type of the created object\n     * @return the newly created object\n     */\n    public static <T> T newInstance(Class<T> clazz, Object... args) {","sourceCodeStart":198,"sourceCodeEnd":234,"githubUrl":"https://github.com/jwtk/jjwt/blob/fb71496164c71442d08adec4571d9616ed5e1b8d/api/src/main/java/io/jsonwebtoken/lang/Classes.java#L198-L234","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Check the Class argument for null before calling Classes.newInstance","Fix the upstream lookup (class name config key, Classes.forName call) that produced null","If a null is expected, guard with a default implementation class"],"exampleFix":"// before\nClass<MyCodec> c = lookupCodecClass(); // may be null\nMyCodec codec = Classes.newInstance(c);\n// after\nClass<MyCodec> c = lookupCodecClass();\nif (c == null) {\n    c = MyCodec.class; // or throw a clearer config error\n}\nMyCodec codec = Classes.newInstance(c);","handlingStrategy":"type-guard","validationCode":"if (clazz == null) {\n    clazz = DefaultImplClass.class; // or fail with a clear config error\n}","typeGuard":"Class<?> resolveClass(Class<?> c) {\n    return (c != null) ? c : DefaultImplClass.class;\n}","tryCatchPattern":"try {\n    return Classes.newInstance(clazz);\n} catch (IllegalArgumentException e) {\n    throw new IllegalStateException(\"Implementation class not configured; cannot instantiate null class\", e);\n}","preventionTips":["Never feed config-derived class names straight into newInstance without a null check","Use typed configuration APIs so missing classes surface early","Document required config keys so lookups cannot return null"],"tags":["null-argument","reflection","precondition"],"backgroundTag":"null-argument","analyzedSha":"fb71496164c71442d08adec4571d9616ed5e1b8d","analyzedAt":"2026-09-09T00:33:09.982Z","contentChangedAt":"2026-09-09T00:33:09.982Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}