mybatis/mybatis-3 · error · TypeException

Error setting null for parameter #" + i + " with JdbcType "

Error message

Error setting null for parameter #" + i + " with JdbcType " + jdbcType + " . Try setting a different JdbcType for this parameter or a different jdbcTypeForNull configuration property. Cause: " + e

What it means

MyBatis successfully decided to call ps.setNull(i, jdbcType.TYPE_CODE) but the JDBC driver itself rejected that type code with a SQLException, which BaseTypeHandler wraps in this TypeException. The original driver error is attached as the cause. The classic case is Oracle refusing java.sql.Types.OTHER for NULL columns.

Source

Thrown at src/main/java/org/apache/ibatis/type/BaseTypeHandler.java:67

   *          the new configuration
   *
   * @deprecated Since 3.5.0 - See https://github.com/mybatis/mybatis-3/issues/1203. This property will remove future.
   */
  @Deprecated
  public void setConfiguration(Configuration c) {
    this.configuration = c;
  }

  @Override
  public void setParameter(PreparedStatement ps, int i, T parameter, JdbcType jdbcType) throws SQLException {
    if (parameter == null) {
      if (jdbcType == null) {
        throw new TypeException("JDBC requires that the JdbcType must be specified for all nullable parameters.");
      }
      try {
        ps.setNull(i, jdbcType.TYPE_CODE);
      } catch (SQLException e) {
        throw new TypeException("Error setting null for parameter #" + i + " with JdbcType " + jdbcType + " . "
            + "Try setting a different JdbcType for this parameter or a different jdbcTypeForNull configuration property. "
            + "Cause: " + e, e);
      }
    } else {
      try {
        setNonNullParameter(ps, i, parameter, jdbcType);
      } catch (Exception e) {
        throw new TypeException("Error setting non null for parameter #" + i + " with JdbcType " + jdbcType + " . "
            + "Try setting a different JdbcType for this parameter or a different configuration property. " + "Cause: "
            + e, e);
      }
    }
  }

  @Override
  public T getResult(ResultSet rs, String columnName) throws SQLException {
    try {
      return getNullableResult(rs, columnName);

View on GitHub (pinned to 008069adb1)

Solutions

  1. Set <setting name="jdbcTypeForNull" value="NULL"/> in the MyBatis configuration (sends Types.NULL instead of OTHER)
  2. Or pin a concrete type on the offending parameter: #{value,jdbcType=VARCHAR}
  3. Read the embedded Cause to confirm which type code the driver rejected and adjust that parameter's jdbcType accordingly

Example fix

// before (mybatis-config.xml)
<settings><setting name="jdbcTypeForNull" value="OTHER"/></settings>

// after
<settings><setting name="jdbcTypeForNull" value="NULL"/></settings>
Defensive patterns

Strategy: try-catch

Try / catch

try { ... } catch (TypeException e) {
  if (e.getCause() instanceof SQLException && e.getMessage().contains("Error setting null")) {
    // switch that parameter to jdbcType=NULL or set jdbcTypeForNull=NULL, then retry
  }
}

Prevention

When it happens

Trigger: A null parameter is sent with jdbcType OTHER (the MyBatis default for jdbcTypeForNull) to a driver that only accepts concrete types — Oracle, older DB2, some SQL Server configurations.

Common situations: Switching an application from MySQL/PostgreSQL (tolerant of OTHER) to Oracle; upgrading a driver that used to silently coerce Types.OTHER; setting a jdbcType in the mapper that the driver's setNull does not support.

Related errors


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