{"record":{"id":"6ed9f635e973b93c","repo":"mybatis/mybatis-3","slug":"error-instantiating-with-invalid-types-or","errorCode":null,"errorMessage":"Error instantiating {} with invalid types ({}) or values ({}). Cause: {}","messagePattern":"Error instantiating (.+?) with invalid types \\((.+?)\\) or values \\((.+?)\\)\\. Cause: (.+?)","errorType":"exception","errorClass":"ReflectionException","httpStatus":null,"severity":"error","filePath":"src/main/java/org/apache/ibatis/reflection/factory/DefaultObjectFactory.java","lineNumber":85,"sourceCode":"          throw e;\n        }\n      }\n      constructor = type.getDeclaredConstructor(constructorArgTypes.toArray(new Class[0]));\n      try {\n        return constructor.newInstance(constructorArgs.toArray(new Object[0]));\n      } catch (IllegalAccessException e) {\n        if (Reflector.canControlMemberAccessible()) {\n          constructor.setAccessible(true);\n          return constructor.newInstance(constructorArgs.toArray(new Object[0]));\n        }\n        throw e;\n      }\n    } catch (Exception e) {\n      String argTypes = Optional.ofNullable(constructorArgTypes).orElseGet(List::of).stream().map(Class::getSimpleName)\n          .collect(Collectors.joining(\",\"));\n      String argValues = Optional.ofNullable(constructorArgs).orElseGet(List::of).stream().map(String::valueOf)\n          .collect(Collectors.joining(\",\"));\n      throw new ReflectionException(\"Error instantiating \" + type + \" with invalid types (\" + argTypes + \") or values (\"\n          + argValues + \"). Cause: \" + e, e);\n    }\n  }\n\n  protected Class<?> resolveInterface(Class<?> type) {\n    Class<?> classToCreate;\n    if (type == List.class || type == Collection.class || type == Iterable.class) {\n      classToCreate = ArrayList.class;\n    } else if (type == Map.class) {\n      classToCreate = HashMap.class;\n    } else if (type == SortedSet.class) { // issue #510 Collections Support\n      classToCreate = TreeSet.class;\n    } else if (type == Set.class) {\n      classToCreate = HashSet.class;\n    } else {\n      classToCreate = type;\n    }\n    return classToCreate;","sourceCodeStart":67,"sourceCodeEnd":103,"githubUrl":"https://github.com/mybatis/mybatis-3/blob/008069adb1b089579b5dcba87ee591908b263274/src/main/java/org/apache/ibatis/reflection/factory/DefaultObjectFactory.java#L67-L103","documentation":"DefaultObjectFactory.instantiateClass() catches every exception from constructor lookup/invocation and rethrows with the exact arg types and values it tried. It means MyBatis could not find or call a constructor of the requested type with the collected arguments (from <constructor> mapping, automapping, or parameter creation).","triggerScenarios":"resultType has no constructor matching the mapped constructor arg types/values; <arg> entries with wrong javaType/jdbcType so no matching constructor exists; constructor threw an exception; interface that resolveInterface does not cover.","commonSituations":"Constructor arg order/types misaligned with the bean's constructors; null passed for a primitive constructor parameter; immutable DTO whose constructor validates and throws; passing an abstract type as resultType.","solutions":["Read the 'invalid types (..) or values (..)' in the message and add/align a constructor with exactly that signature.","Fix <constructor><arg javaType=...> declarations (order and types) to match an existing constructor.","Guard constructor args for null when the target parameter is primitive, or make the parameter a wrapper type.","If the constructor itself throws, fix the exception shown in 'Cause:'."],"exampleFix":"<!-- before -->\n<constructor>\n  <arg column=\"name\" javaType=\"string\"/>\n  <arg column=\"age\" javaType=\"_int\"/>\n</constructor>\n<!-- User(String, Integer) missing; User(Integer, String) exists -->\n<!-- after -->\n<constructor>\n  <arg column=\"age\" javaType=\"_int\"/>\n  <arg column=\"name\" javaType=\"string\"/>\n</constructor>","handlingStrategy":"try-catch","validationCode":"Class<?>[] wanted = argTypes.toArray(new Class<?>[0]);\nboolean found = Arrays.stream(User.class.getConstructors())\n    .anyMatch(c -> Arrays.equals(c.getParameterTypes(), wanted));\nif (!found) { /* adjust mapping or add constructor before running the query */ }","typeGuard":null,"tryCatchPattern":"try {\n  return sqlSession.selectOne(\"findUser\", id);\n} catch (ReflectionException e) {\n  if (e.getMessage().startsWith(\"Error instantiating\")) {\n    // parse 'invalid types (..) or values (..)' and align the constructor mapping\n  }\n  throw e;\n}","preventionTips":["Mirror constructor <arg> lists 1:1 with an actual constructor and add a unit test that instantiates the DTO with the same arg types.","Avoid nulls for primitive constructor params; use wrapper types in the DTO."],"tags":["mybatis","reflection","constructor","instantiation","resultmap"],"backgroundTag":null,"analyzedSha":"008069adb1b089579b5dcba87ee591908b263274","analyzedAt":"2026-08-14T13:07:10.264Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}