apache/flink · error · NullPointerException
Key type must not be null.
Error message
Key type must not be null.
What it means
Thrown by the SelectorFunctionKeys constructor when keyType is null. The key type information drives comparator and serializer selection; without it Flink cannot build state/partitioning machinery, so it is rejected with a NullPointerException before any other validation.
Source
Thrown at flink-core/src/main/java/org/apache/flink/api/common/operators/Keys.java:99
public static class SelectorFunctionKeys<T, K> extends Keys<T> {
private final KeySelector<T, K> keyExtractor;
private final TypeInformation<T> inputType;
private final TypeInformation<K> keyType;
private final List<FlatFieldDescriptor> keyFields;
private final TypeInformation<?>[] originalKeyTypes;
public SelectorFunctionKeys(
KeySelector<T, K> keyExtractor,
TypeInformation<T> inputType,
TypeInformation<K> keyType) {
if (keyExtractor == null) {
throw new NullPointerException("Key extractor must not be null.");
}
if (keyType == null) {
throw new NullPointerException("Key type must not be null.");
}
if (!keyType.isKeyType()) {
throw new InvalidProgramException(
"Return type "
+ keyType
+ " of KeySelector "
+ keyExtractor.getClass()
+ " is not a valid key type");
}
this.keyExtractor = keyExtractor;
this.inputType = inputType;
this.keyType = keyType;
this.originalKeyTypes = new TypeInformation[] {keyType};
if (keyType instanceof CompositeType) {
this.keyFields =
((CompositeType<T>) keyType).getFlatFields(ExpressionKeys.SELECT_ALL_CHAR);View on GitHub (pinned to 2f3c205e92)
Solutions
- Provide an explicit TypeInformation for the key (e.g. Types.STRING, Types.LONG).
- If constructing via the API, add a .returns(...) hint so Flink can infer the key type.
- Use Objects.requireNonNull(keyType) before construction to fail with your own message.
Example fix
// before new SelectorFunctionKeys<>(sel, inType, null); // after new SelectorFunctionKeys<>(sel, inType, Types.STRING);
Defensive patterns
Strategy: validation
Validate before calling
TypeInformation<K> kt = Objects.requireNonNull(keyType, "keyType"); new SelectorFunctionKeys<>(keySelector, inputType, kt);
Type guard
static <K> boolean hasKeyType(TypeInformation<K> t) { return t != null; } Prevention
- Always pass an explicit TypeInformation for the key, especially with generic KeySelectors.
- Add a .returns(...) hint so Flink can infer the key type.
- Centralise type construction so nulls surface as compile-time gaps.
When it happens
Trigger: Constructing SelectorFunctionKeys with a null TypeInformation for the key, often because TypeExtractor returned null for an unresolvable generic KeySelector return type, or the caller passed an uninitialised type field.
Common situations: Custom KeySelector implementations with erased generics where TypeInformation cannot be extracted; omitting the returns(...) hint on a keyBy that Flink cannot introspect; passing a programmatically-built type that defaulted to null.
Related errors
- Key extractor must not be null.
- Return type {keyType} of KeySelector {keyExtractor.getClass(
- Expression key may not be null.
- The broadcast input name may not be null.
- The broadcast input root operator may not be null.
AI-assisted analysis of apache/flink@2f3c205e92 (2026-08-14).
Data as JSON: /api/errors/8ced35c56049e90f.
Report an issue: GitHub.