{"record":{"id":"c8e4bba0d94a8902","repo":"didi/DoKit","slug":"constructor-was-null","errorCode":null,"errorMessage":"Constructor was null!","messagePattern":"Constructor was null!","errorType":"exception","errorClass":"ReflectedException","httpStatus":null,"severity":"error","filePath":"Android/dokit/src/main/java/com/didichuxing/doraemonkit/util/Reflector.java","lineNumber":81,"sourceCode":"    \n    }\n    \n    public Reflector constructor(@Nullable Class<?>... parameterTypes) throws ReflectedException {\n        try {\n            mConstructor = mType.getDeclaredConstructor(parameterTypes);\n            mConstructor.setAccessible(true);\n            mField = null;\n            mMethod = null;\n            return this;\n        } catch (Throwable e) {\n            throw new ReflectedException(\"Oops!\", e);\n        }\n    }\n    \n    @SuppressWarnings(\"unchecked\")\n    public <R> R newInstance(@Nullable Object ... initargs) throws ReflectedException {\n        if (mConstructor == null) {\n            throw new ReflectedException(\"Constructor was null!\");\n        }\n        try {\n            return (R) mConstructor.newInstance(initargs);\n        } catch (InvocationTargetException e) {\n            throw new ReflectedException(\"Oops!\", e.getTargetException());\n        } catch (Throwable e) {\n            throw new ReflectedException(\"Oops!\", e);\n        }\n    }\n    \n    protected Object checked(@Nullable Object caller) throws ReflectedException {\n        if (caller == null || mType.isInstance(caller)) {\n            return caller;\n        }\n        throw new ReflectedException(\"Caller [\" + caller + \"] is not a instance of type [\" + mType + \"]!\");\n    }\n    \n    protected void check(@Nullable Object caller, @Nullable Member member, @NonNull String name) throws ReflectedException {","sourceCodeStart":63,"sourceCodeEnd":99,"githubUrl":"https://github.com/didi/DoKit/blob/626827cddb2feb2f3aee87a52a064b4e5ca2bed4/Android/dokit/src/main/java/com/didichuxing/doraemonkit/util/Reflector.java#L63-L99","documentation":"Reflector.newInstance(Object...) throws ReflectedException(\"Constructor was null!\") when the internal mConstructor field is null — i.e. newInstance is called before constructor(...) ever successfully selected one, or after a later field()/method() call reset mConstructor to null. It is a strict usage-ordering guard, not a JVM failure.","triggerScenarios":"Calling Reflector.on(SomeClass.class).newInstance(...) directly without a preceding .constructor(...); or calling .field(\"x\")/.method(\"y\") after .constructor(...) (both null out mConstructor) and then newInstance again.","commonSituations":"Chained fluent calls reordered during refactoring; copy-paste of a reflect snippet that omitted the constructor step; interleaving constructor and field/method lookups on the same Reflector instance.","solutions":["Always chain .constructor(parameterTypes) immediately before .newInstance(args)","Do not interleave .field()/.method() between .constructor() and .newInstance() — they clear mConstructor","Use a fresh Reflector.on(...) instance per member kind if you need several lookups"],"exampleFix":"// before\nReflector.on(cls).newInstance(\"arg\");\n\n// after\nReflector.on(cls).constructor(String.class).newInstance(\"arg\");","handlingStrategy":"validation","validationCode":"// ensure constructor is selected before newInstance\nif (r == null /* or mConstructor unset */) { throw new IllegalStateException(\"call constructor(...) first\"); }","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Keep .constructor(...).newInstance(...) in one fluent chain","Use one Reflector instance per member kind; field()/method() clear mConstructor"],"tags":["reflection","api-misuse","ordering","null-guard"],"backgroundTag":null,"analyzedSha":"626827cddb2feb2f3aee87a52a064b4e5ca2bed4","analyzedAt":"2026-08-14T12:45:58.758Z","schemaVersion":2},"datasetVersion":"2026-08-16T03:17:38.424Z"}