{"record":{"id":"abcb1fe3e89cc2c0","repo":"jwtk/jjwt","slug":"unable-to-instantiate-instance-with-constructor","errorCode":null,"errorMessage":"Unable to instantiate instance with constructor [${ctor}]","messagePattern":"Unable to instantiate instance with constructor \\[(.+?)\\]","errorType":"exception","errorClass":"InstantiationException","httpStatus":null,"severity":"error","filePath":"api/src/main/java/io/jsonwebtoken/lang/Classes.java","lineNumber":277,"sourceCode":"\n    }\n\n    /**\n     * Creates a new object using the specified {@link Constructor}, invoking it with the specified constructor\n     * {@code args} arguments.\n     *\n     * @param ctor the constructor to invoke\n     * @param args the arguments to supply to the constructor\n     * @param <T>  the type of object to create\n     * @return the new object instance\n     * @throws InstantiationException if the constructor cannot be invoked successfully\n     */\n    public static <T> T instantiate(Constructor<T> ctor, Object... args) {\n        try {\n            return ctor.newInstance(args);\n        } catch (Exception e) {\n            String msg = \"Unable to instantiate instance with constructor [\" + ctor + \"]\";\n            throw new InstantiationException(msg, e);\n        }\n    }\n\n    /**\n     * Invokes the fully qualified class name's method named {@code methodName} with parameters of type {@code argTypes}\n     * using the {@code args} as the method arguments.\n     *\n     * @param fqcn       fully qualified class name to locate\n     * @param methodName name of the method to invoke on the class\n     * @param argTypes   the method argument types supported by the {@code methodName} method\n     * @param args       the runtime arguments to use when invoking the located class method\n     * @param <T>        the expected type of the object returned from the invoked method.\n     * @return the result returned by the invoked method\n     * @since 0.10.0\n     */\n    public static <T> T invokeStatic(String fqcn, String methodName, Class<?>[] argTypes, Object... args) {\n        try {\n            Class<?> clazz = Classes.forName(fqcn);","sourceCodeStart":259,"sourceCodeEnd":295,"githubUrl":"https://github.com/jwtk/jjwt/blob/fb71496164c71442d08adec4571d9616ed5e1b8d/api/src/main/java/io/jsonwebtoken/lang/Classes.java#L259-L295","documentation":"InstantiationException thrown by io.jsonwebtoken.lang.Classes.instantiate(Constructor, Object...) when Constructor.newInstance(args) fails. Any exception from the constructor (access, argument mismatch, or constructor-internal failure) is wrapped with a message naming the constructor. Called by Classes.newInstance(Class, Object...).","triggerScenarios":"Calling Classes.newInstance(clazz, args) where the resolved constructor throws (including InvocationTargetException wrapping a constructor-internal exception), the args do not match the constructor signature, or the constructor is inaccessible.","commonSituations":"Passing wrong argument types/count for the configured class's constructor; constructor validation throwing (e.g. null key); reflective instantiation of classes with package-private constructors; JDK 9+ module access restrictions.","solutions":["Inspect the cause (getCause) for the real constructor failure","Match the argument list exactly to the constructor signature the library selects","Make the constructor public and, for JDK 9+, open the relevant package (--add-opens or module exports)","Fix validation logic inside the target constructor that is throwing"],"exampleFix":"// before\nJwtParser p = Classes.newInstance(ParserClass.class, wrongArg); // arg type mismatch\n// after\nJwtParser p = Classes.newInstance(ParserClass.class, expectedTypedArg);","handlingStrategy":"try-catch","validationCode":"for (Constructor<?> ct : clazz.getConstructors()) {\n    if (Arrays.equals(ct.getParameterTypes(), argTypes)) return; // signature OK\n}\nthrow new IllegalArgumentException(\"No matching constructor on \" + clazz);","typeGuard":null,"tryCatchPattern":"try {\n    return Classes.newInstance(clazz, args);\n} catch (InstantiationException e) {\n    Throwable root = e.getCause();\n    throw new IllegalStateException(\"Constructor failed: \" + (root != null ? root.getMessage() : e.getMessage()), e);\n}","preventionTips":["Match argument types and count exactly to the constructor signature","Keep custom constructors simple and side-effect free so they cannot throw","Open required packages with --add-opens on JDK 9+ if instantiating library internals"],"tags":["reflection","constructor","instantiation"],"backgroundTag":"invalid-constructor-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"}