mybatis/mybatis-3 · error · ReflectionException

Error instantiating {} with invalid types ({}) or values ({}

Error message

Error instantiating {} with invalid types ({}) or values ({}). Cause: {}

What it means

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).

Source

Thrown at src/main/java/org/apache/ibatis/reflection/factory/DefaultObjectFactory.java:85

          throw e;
        }
      }
      constructor = type.getDeclaredConstructor(constructorArgTypes.toArray(new Class[0]));
      try {
        return constructor.newInstance(constructorArgs.toArray(new Object[0]));
      } catch (IllegalAccessException e) {
        if (Reflector.canControlMemberAccessible()) {
          constructor.setAccessible(true);
          return constructor.newInstance(constructorArgs.toArray(new Object[0]));
        }
        throw e;
      }
    } catch (Exception e) {
      String argTypes = Optional.ofNullable(constructorArgTypes).orElseGet(List::of).stream().map(Class::getSimpleName)
          .collect(Collectors.joining(","));
      String argValues = Optional.ofNullable(constructorArgs).orElseGet(List::of).stream().map(String::valueOf)
          .collect(Collectors.joining(","));
      throw new ReflectionException("Error instantiating " + type + " with invalid types (" + argTypes + ") or values ("
          + argValues + "). Cause: " + e, e);
    }
  }

  protected Class<?> resolveInterface(Class<?> type) {
    Class<?> classToCreate;
    if (type == List.class || type == Collection.class || type == Iterable.class) {
      classToCreate = ArrayList.class;
    } else if (type == Map.class) {
      classToCreate = HashMap.class;
    } else if (type == SortedSet.class) { // issue #510 Collections Support
      classToCreate = TreeSet.class;
    } else if (type == Set.class) {
      classToCreate = HashSet.class;
    } else {
      classToCreate = type;
    }
    return classToCreate;

View on GitHub (pinned to 008069adb1)

Solutions

  1. Read the 'invalid types (..) or values (..)' in the message and add/align a constructor with exactly that signature.
  2. Fix <constructor><arg javaType=...> declarations (order and types) to match an existing constructor.
  3. Guard constructor args for null when the target parameter is primitive, or make the parameter a wrapper type.
  4. If the constructor itself throws, fix the exception shown in 'Cause:'.

Example fix

<!-- before -->
<constructor>
  <arg column="name" javaType="string"/>
  <arg column="age" javaType="_int"/>
</constructor>
<!-- User(String, Integer) missing; User(Integer, String) exists -->
<!-- after -->
<constructor>
  <arg column="age" javaType="_int"/>
  <arg column="name" javaType="string"/>
</constructor>
Defensive patterns

Strategy: try-catch

Validate before calling

Class<?>[] wanted = argTypes.toArray(new Class<?>[0]);
boolean found = Arrays.stream(User.class.getConstructors())
    .anyMatch(c -> Arrays.equals(c.getParameterTypes(), wanted));
if (!found) { /* adjust mapping or add constructor before running the query */ }

Try / catch

try {
  return sqlSession.selectOne("findUser", id);
} catch (ReflectionException e) {
  if (e.getMessage().startsWith("Error instantiating")) {
    // parse 'invalid types (..) or values (..)' and align the constructor mapping
  }
  throw e;
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of mybatis/mybatis-3@008069adb1 (2026-08-14). Data as JSON: /api/errors/6ed9f635e973b93c. Report an issue: GitHub.