mybatis/mybatis-3 · error · IllegalArgumentException

Type argument cannot be null

Error message

Type argument cannot be null

What it means

EnumTypeHandler (name-based enum mapping) stores the enum Class to convert names back to constants. Constructing it with a null Class throws this IllegalArgumentException from the constructor — the handler cannot operate on an unknown type.

Source

Thrown at src/main/java/org/apache/ibatis/type/EnumTypeHandler.java:32

 *    limitations under the License.
 */
package org.apache.ibatis.type;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @author Clinton Begin
 */
public class EnumTypeHandler<E extends Enum<E>> extends BaseTypeHandler<E> {

  private final Class<E> type;

  public EnumTypeHandler(Class<E> type) {
    if (type == null) {
      throw new IllegalArgumentException("Type argument cannot be null");
    }
    this.type = type;
  }

  @Override
  public void setNonNullParameter(PreparedStatement ps, int i, E parameter, JdbcType jdbcType) throws SQLException {
    if (jdbcType == null) {
      ps.setString(i, parameter.name());
    } else {
      ps.setObject(i, parameter.name(), jdbcType.TYPE_CODE); // see r3589
    }
  }

  @Override
  public E getNullableResult(ResultSet rs, String columnName) throws SQLException {
    String s = rs.getString(columnName);
    return s == null ? null : Enum.valueOf(type, s);
  }

View on GitHub (pinned to 008069adb1)

Solutions

  1. Add javaType to the registration: <typeHandler handler="...EnumTypeHandler" javaType="com.example.MyEnum"/>
  2. Give the mapper property an explicit javaType (#{status,javaType=com.example.MyEnum})
  3. When constructing manually, pass the non-null enum class

Example fix

// before
new EnumTypeHandler<>(null);

// after
new EnumTypeHandler<>(Status.class);
Defensive patterns

Strategy: validation

Validate before calling

if (enumClass == null) throw new IllegalArgumentException("enumClass required for EnumTypeHandler");
return new EnumTypeHandler<>(enumClass);

Type guard

static <E extends Enum<E>> EnumTypeHandler<E> safeNameHandler(Class<E> type) {
  return type == null ? null : new EnumTypeHandler<>(type);
}

Prevention

When it happens

Trigger: new EnumTypeHandler<>(null); or a <typeHandler handler="...EnumTypeHandler"/> registration without javaType, causing TypeHandlerRegistry to instantiate it with a null type; unresolved java types on enum properties.

Common situations: Registering EnumTypeHandler in XML without the javaType attribute; a parameter or property whose generic type was erased so MyBatis could not infer the enum class.

Related errors


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