mybatis/mybatis-3 · error · NullPointerException

dataSource cannot be null

Error message

dataSource cannot be null

What it means

VendorDatabaseIdProvider.getDatabaseId() throws NullPointerException when the passed DataSource is null. The provider must open a connection to read DatabaseMetaData.getDatabaseProductName(), so a null DataSource cannot be handled.

Source

Thrown at src/main/java/org/apache/ibatis/mapping/VendorDatabaseIdProvider.java:42

import org.apache.ibatis.builder.BuilderException;

/**
 * Vendor DatabaseId provider.
 * <p>
 * It returns database product name as a databaseId. If the user provides a properties it uses it to translate database
 * product name key="Microsoft SQL Server", value="ms" will return "ms". It can return null, if no database product name
 * or a properties was specified and no translation was found.
 *
 * @author Eduardo Macarron
 */
public class VendorDatabaseIdProvider implements DatabaseIdProvider {

  private Properties properties;

  @Override
  public String getDatabaseId(DataSource dataSource) {
    if (dataSource == null) {
      throw new NullPointerException("dataSource cannot be null");
    }
    try {
      return getDatabaseName(dataSource);
    } catch (SQLException e) {
      throw new BuilderException("Error occurred when getting DB product name.", e);
    }
  }

  @Override
  public void setProperties(Properties p) {
    this.properties = p;
  }

  private String getDatabaseName(DataSource dataSource) throws SQLException {
    String productName = getDatabaseProductName(dataSource);
    if (properties == null || properties.isEmpty()) {
      return productName;
    }

View on GitHub (pinned to 008069adb1)

Solutions

  1. Ensure the Environment with a non-null DataSource is set on the Configuration before the databaseIdProvider resolves (defaultEnvironmentRef points at a real environment).
  2. If calling the provider directly, pass the actual DataSource instance.
  3. Null-check the DataSource before invoking getDatabaseId if it may legitimately be absent.

Example fix

// before
String dbId = provider.getDatabaseId(null);
// after
DataSource ds = configuration.getEnvironment().getDataSource();
String dbId = provider.getDatabaseId(ds);
Defensive patterns

Strategy: type-guard

Validate before calling

if (dataSource == null) throw new IllegalStateException("Environment/DataSource not configured before databaseId resolution");

Type guard

DataSource ds = configuration.getEnvironment() != null ? configuration.getEnvironment().getDataSource() : null;
if (ds == null) { /* skip databaseId resolution */ }

Prevention

When it happens

Trigger: Calling new VendorDatabaseIdProvider().getDatabaseId(null), or building a Configuration/XMLConfigBuilder where a databaseIdProvider is configured but the environment/DataSource is missing so MyBatis passes null.

Common situations: Configuration built programmatically and dataSource set after the databaseIdProvider is applied; XML config with <databaseIdProvider> but a missing or unnamed <environment> that leaves dataSource null during parsing.

Related errors


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