{"record":{"id":"1dad3c87c87be67f","repo":"apache/pulsar","slug":"could-not-instantiate-cls-getname-either-with","errorCode":null,"errorMessage":"Could not instantiate ${cls.getName()} either with or without construction parameter ${outer}: ${ex}","messagePattern":"Could not instantiate (.+?) either with or without construction parameter (.+?): (.+?)","errorType":"exception","errorClass":"InitializationException","httpStatus":null,"severity":"error","filePath":"pulsar-client-tools/src/main/java/org/apache/pulsar/internal/InnerClassFactory.java","lineNumber":53,"sourceCode":"\n    public <K> K create(final Class<K> cls) throws Exception {\n        try {\n            return defaultFactory.create(cls);\n        } catch (Exception ex0) {\n            try {\n                Constructor<K> constructor = cls.getDeclaredConstructor(outer.getClass());\n                return constructor.newInstance(outer);\n            } catch (Exception ex) {\n                try {\n                    @SuppressWarnings(\"deprecation\") // Class.newInstance is deprecated in Java 9\n                    K result = cls.newInstance();\n                    return result;\n                } catch (Exception ex2) {\n                    try {\n                        Constructor<K> constructor = cls.getDeclaredConstructor();\n                        return constructor.newInstance();\n                    } catch (Exception ex3) {\n                        throw new InitializationException(\"Could not instantiate \" + cls.getName()\n                                + \" either with or without construction parameter \" + outer + \": \" + ex, ex);\n                    }\n                }\n            }\n        }\n    }\n}\n","sourceCodeStart":35,"sourceCodeEnd":61,"githubUrl":"https://github.com/apache/pulsar/blob/820761864ed8e2a7d2e52dd9763ad2ae117c1395/pulsar-client-tools/src/main/java/org/apache/pulsar/internal/InnerClassFactory.java#L35-L61","documentation":"Pulsar's InnerClassFactory tries to instantiate a config/args class reflectively: first via a constructor taking the outer instance (for non-static inner classes), then via a no-arg declared constructor. If both attempts fail, it wraps the first exception in InitializationException describing both attempts. Typically the class lacks a usable no-arg constructor (e.g. non-static inner class or constructor requiring args).","triggerScenarios":"Passing a class to create() that is a non-static inner class without an accessible outer instance, a class whose no-arg constructor throws, or a class whose declared constructor is inaccessible (security/visibility) — the reflective newInstance calls fail.","commonSituations":"Defining CLI @Parameters classes as inner classes without static; constructor performing failing initialization (throwing in <init>); module/JDK access restrictions blocking setAccessible; typo passing the wrong Class object.","solutions":["Make the parameter class a static nested class (or top-level) so a no-arg constructor suffices.","Ensure the class has a public no-arg constructor and does not throw during construction.","Inspect the nested cause (ex/ex3) in the InitializationException to see why instantiation failed.","If it must be an inner class, provide the correct outer instance so the outer-parameter constructor succeeds."],"exampleFix":"// before\nclass MyArgs { }            // non-static inner class inside another class\n// after\nstatic class MyArgs { }     // static nested class, instantiable with no-arg ctor","handlingStrategy":"try-catch","validationCode":"// Java: check the class is instantiable before use\nstatic boolean instantiable(Class<?> c) {\n  return java.lang.reflect.Modifier.isStatic(c.getModifiers()) || c.getDeclaringClass() == null;\n}","typeGuard":"static boolean hasNoArgCtor(Class<?> c) { try { c.getDeclaredConstructor(); return true; } catch (NoSuchMethodException e) { return false; } }","tryCatchPattern":"try { T cfg = factory.create(cls, outer); } catch (InitializationException e) { LOG.error(\"cannot instantiate {}: {}\", cls, e.getCause(), e); /* use a default instance or abort with a clear message */ }","preventionTips":["Declare nested config/parameter classes as static.","Provide a public no-arg constructor and avoid throwing in constructors.","Log/inspect the cause chain — the original exception explains which constructor failed."],"tags":["reflection","instantiation","initialization","pulsar"],"backgroundTag":"class-instantiation-failed","analyzedSha":"820761864ed8e2a7d2e52dd9763ad2ae117c1395","analyzedAt":"2026-09-06T00:14:20.138Z","contentChangedAt":"2026-09-06T00:14:20.138Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}