mybatis/mybatis-3 · error · IllegalArgumentException
Parameter 'id' must not be null
Error message
Parameter 'id' must not be null
What it means
Environment's constructor throws IllegalArgumentException when id is null. The id names the environment (e.g. 'development', 'production') inside the MyBatis Configuration and must be non-null even before transactionFactory/dataSource are checked.
Source
Thrown at src/main/java/org/apache/ibatis/mapping/Environment.java:32
* limitations under the License.
*/
package org.apache.ibatis.mapping;
import javax.sql.DataSource;
import org.apache.ibatis.transaction.TransactionFactory;
/**
* @author Clinton Begin
*/
public final class Environment {
private final String id;
private final TransactionFactory transactionFactory;
private final DataSource dataSource;
public Environment(String id, TransactionFactory transactionFactory, DataSource dataSource) {
if (id == null) {
throw new IllegalArgumentException("Parameter 'id' must not be null");
}
if (transactionFactory == null) {
throw new IllegalArgumentException("Parameter 'transactionFactory' must not be null");
}
this.id = id;
if (dataSource == null) {
throw new IllegalArgumentException("Parameter 'dataSource' must not be null");
}
this.transactionFactory = transactionFactory;
this.dataSource = dataSource;
}
public static class Builder {
private final String id;
private TransactionFactory transactionFactory;
private DataSource dataSource;
public Builder(String id) {View on GitHub (pinned to 008069adb1)
Solutions
- Supply a non-null environment id, e.g. new Environment("development", factory, ds) or the XML default.
- Trace where the null came from: log/validate the config lookup (key name, properties file loaded) before constructing.
- If converting from XML, make sure the <environments default=...> id is carried over.
Example fix
// before
String envId = props.getProperty("env.name"); // key misspelled -> null
new Environment(envId, txFactory, ds);
// after
String envId = props.getProperty("env.name");
if (envId == null) throw new IllegalStateException("env.name missing in config");
new Environment(envId, txFactory, ds); Defensive patterns
Strategy: validation
Validate before calling
String envId = Objects.requireNonNull(config.get("env.id"), "env.id must be configured");
Environment env = new Environment(envId, txFactory, ds); Prevention
- Fail fast with a descriptive message when reading the environment name from external config.
- Use constants for environment ids instead of stringly-typed lookups scattered in code.
- Cover config loading with a startup smoke test that builds the Environment.
When it happens
Trigger: new Environment(null, txFactory, dataSource), or Environment.Builder with a null id string — commonly the id comes from a variable/config that was never set (null from a properties file lookup that silently failed).
Common situations: Building a Configuration programmatically from external config where the environment-name key is misspelled so the lookup returns null; refactoring XML <environments> to Java config and dropping the id.
Related errors
- Parameter 'transactionFactory' must not be null
- Parameter 'dataSource' must not be null
- ResultMaps must have an id
- dataSource cannot be null
- Invalid bound statement (not found): {mapperInterface}.{meth
AI-assisted analysis of mybatis/mybatis-3@008069adb1 (2026-08-14).
Data as JSON: /api/errors/3fa62bd80a490fcb.
Report an issue: GitHub.