apache/flink · error · NullPointerException
Mapper may not be null.
Error message
Mapper may not be null.
What it means
Thrown by the HadoopMapFunction constructor when the wrapped Hadoop mapred Mapper argument is null. This is a hard precondition: a null mapper cannot be serialized or executed, so construction fails fast with a NullPointerException rather than failing later during open()/flatMap().
Source
Thrown at flink-connectors/flink-hadoop-compatibility/src/main/java/org/apache/flink/hadoopcompatibility/mapred/HadoopMapFunction.java:77
/**
* Maps a Hadoop Mapper (mapred API) to a Flink FlatMapFunction.
*
* @param hadoopMapper The Hadoop Mapper to wrap.
*/
public HadoopMapFunction(Mapper<KEYIN, VALUEIN, KEYOUT, VALUEOUT> hadoopMapper) {
this(hadoopMapper, new JobConf());
}
/**
* Maps a Hadoop Mapper (mapred API) to a Flink FlatMapFunction. The Hadoop Mapper is configured
* with the provided JobConf.
*
* @param hadoopMapper The Hadoop Mapper to wrap.
* @param conf The JobConf that is used to configure the Hadoop Mapper.
*/
public HadoopMapFunction(Mapper<KEYIN, VALUEIN, KEYOUT, VALUEOUT> hadoopMapper, JobConf conf) {
if (hadoopMapper == null) {
throw new NullPointerException("Mapper may not be null.");
}
if (conf == null) {
throw new NullPointerException("JobConf may not be null.");
}
this.mapper = hadoopMapper;
this.jobConf = conf;
}
@PublicEvolving
@Override
public void open(OpenContext openContext) throws Exception {
super.open(openContext);
this.mapper.configure(jobConf);
this.reporter = new HadoopDummyReporter();
this.outputCollector = new HadoopOutputCollector<KEYOUT, VALUEOUT>();
}View on GitHub (pinned to 2f3c205e92)
Solutions
- Ensure the Mapper passed to HadoopMapFunction is non-null and fully constructed.
- If a mapper factory can return null, guard it and fail or supply a default mapper instead.
- In tests, instantiate a real or stub Mapper rather than passing null.
- Add a null-check at the call site with a clear message before constructing the wrapper.
Example fix
// before
Mapper mapper = mapperFactory.get(); // may be null
new HadoopMapFunction(mapper, conf);
// after — guard against null
Mapper mapper = mapperFactory.get();
if (mapper == null) throw new IllegalStateException("mapperFactory returned null");
new HadoopMapFunction(mapper, conf); Defensive patterns
Strategy: validation
Validate before calling
java.util.Objects.requireNonNull(hadoopMapper, "Hadoop Mapper must not be null"); new HadoopMapFunction(hadoopMapper, conf);
Type guard
hadoopMapper != null
Prevention
- Guard the mapper reference with Objects.requireNonNull before constructing.
- Ensure mapper factories never return null.
- In tests, instantiate a real or stub Mapper.
- Fail loud at wiring time rather than relying on the constructor NPE.
When it happens
Trigger: Produced when new HadoopMapFunction(hadoopMapper, conf) (or the single-arg overload that delegates to it) is called with hadoopMapper == null — e.g. a mapper field that was never initialized, a factory returning null, or test wiring that passes null.
Common situations: A Mapper field left null due to a wiring/injection bug; a factory method that can return null under some conditions; copy-paste constructor calls passing the wrong (null) reference; test scaffolding that forgets to set the mapper.
Related errors
- JobConf may not be null.
- Hadoop input split must not be null
- The collection contains a null element
- The collection contains a null element
- Unable to instantiate Hadoop InputSplit
AI-assisted analysis of apache/flink@2f3c205e92 (2026-08-14).
Data as JSON: /api/errors/3d80453fd7b184e8.
Report an issue: GitHub.