{"record":{"id":"ca2dffedc1adb061","repo":"mybatis/mybatis-3","slug":"error-creating-instance-cause-cause","errorCode":null,"errorMessage":"Error creating instance. Cause: {cause}","messagePattern":"Error creating instance\\. Cause: (.+?)","errorType":"exception","errorClass":"BuilderException","httpStatus":null,"severity":"error","filePath":"src/main/java/org/apache/ibatis/builder/BaseBuilder.java","lineNumber":96,"sourceCode":"    } catch (IllegalArgumentException e) {\n      throw new BuilderException(\"Error resolving ResultSetType. Cause: \" + e, e);\n    }\n  }\n\n  protected ParameterMode resolveParameterMode(String alias) {\n    try {\n      return alias == null ? null : ParameterMode.valueOf(alias);\n    } catch (IllegalArgumentException e) {\n      throw new BuilderException(\"Error resolving ParameterMode. Cause: \" + e, e);\n    }\n  }\n\n  protected Object createInstance(String alias) {\n    Class<?> clazz = resolveClass(alias);\n    try {\n      return clazz == null ? null : clazz.getDeclaredConstructor().newInstance();\n    } catch (Exception e) {\n      throw new BuilderException(\"Error creating instance. Cause: \" + e, e);\n    }\n  }\n\n  protected <T> Class<? extends T> resolveClass(String alias) {\n    try {\n      return alias == null ? null : resolveAlias(alias);\n    } catch (Exception e) {\n      throw new BuilderException(\"Error resolving class. Cause: \" + e, e);\n    }\n  }\n\n  @Deprecated(since = \"3.6.0\", forRemoval = true)\n  protected TypeHandler<?> resolveTypeHandler(Class<?> javaType, String typeHandlerAlias) {\n    return resolveTypeHandler(javaType, null, typeHandlerAlias);\n  }\n\n  @Deprecated(since = \"3.6.0\", forRemoval = true)\n  protected TypeHandler<?> resolveTypeHandler(Class<?> javaType, Class<? extends TypeHandler<?>> typeHandlerType) {","sourceCodeStart":78,"sourceCodeEnd":114,"githubUrl":"https://github.com/mybatis/mybatis-3/blob/008069adb1b089579b5dcba87ee591908b263274/src/main/java/org/apache/ibatis/builder/BaseBuilder.java#L78-L114","documentation":"BaseBuilder.createInstance(alias) resolves an alias/short name to a Class and then calls its declared no-arg constructor via reflection. Any constructor failure - missing no-arg constructor, non-public constructor, or constructor that throws - is wrapped into this BuilderException. It instantiates pluggable components named in configuration: objectFactory, objectWrapperFactory, reflectorFactory, typeHandler, javaType wrappers, cache implementations, etc.","triggerScenarios":"typeHandler=\"com.example.MyHandler\" where MyHandler has no public no-arg constructor; an objectFactory class whose constructor throws; defaultCache or eviction/class implementations without accessible constructors; class found but in a module/OJPMS context where the constructor is not exported.","commonSituations":"Custom TypeHandler or Cache written with only a parameterized constructor; constructor performing initialization that fails (e.g. reads a resource that is absent); Java 16+ strong encapsulation blocking reflective access.","solutions":["Read the chained 'Cause:' to identify the constructor failure, then add a public no-arg constructor to the class","If the constructor throws, fix the underlying initialization error (missing resource, null config, etc.)","Ensure the class and its constructor are public and accessible to the MyBatis classloader"],"exampleFix":"// before\npublic class MyHandler extends BaseTypeHandler<String> {\n  public MyHandler(DataSource ds) { ... }\n}\n<!-- after -->\npublic class MyHandler extends BaseTypeHandler<String> {\n  public MyHandler() { ... }\n  public MyHandler(DataSource ds) { this(); ... }\n}","handlingStrategy":"validation","validationCode":"Class<?> c = Class.forName(\"com.example.MyHandler\");\nif (!Modifier.isPublic(c.getModifiers())) throw new IllegalStateException(\"handler must be public\");\nc.getDeclaredConstructor(); // throws NoSuchMethodException if no no-arg constructor exists","typeGuard":null,"tryCatchPattern":"catch (BuilderException e) { log.error(\"instantiation failed: {}\", e.getCause()); throw e; }","preventionTips":["Give every pluggable component (TypeHandler, Cache, ObjectFactory) a public no-arg constructor","Keep constructors side-effect free; move heavy init to lifecycle methods"],"tags":["mybatis","builder","reflection","instantiation","configuration"],"backgroundTag":null,"analyzedSha":"008069adb1b089579b5dcba87ee591908b263274","analyzedAt":"2026-08-14T13:07:10.264Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}