{"record":{"id":"8ced35c56049e90f","repo":"apache/flink","slug":"key-type-must-not-be-null","errorCode":null,"errorMessage":"Key type must not be null.","messagePattern":"Key type must not be null\\.","errorType":"exception","errorClass":"NullPointerException","httpStatus":null,"severity":"error","filePath":"flink-core/src/main/java/org/apache/flink/api/common/operators/Keys.java","lineNumber":99,"sourceCode":"\n    public static class SelectorFunctionKeys<T, K> extends Keys<T> {\n\n        private final KeySelector<T, K> keyExtractor;\n        private final TypeInformation<T> inputType;\n        private final TypeInformation<K> keyType;\n        private final List<FlatFieldDescriptor> keyFields;\n        private final TypeInformation<?>[] originalKeyTypes;\n\n        public SelectorFunctionKeys(\n                KeySelector<T, K> keyExtractor,\n                TypeInformation<T> inputType,\n                TypeInformation<K> keyType) {\n\n            if (keyExtractor == null) {\n                throw new NullPointerException(\"Key extractor must not be null.\");\n            }\n            if (keyType == null) {\n                throw new NullPointerException(\"Key type must not be null.\");\n            }\n            if (!keyType.isKeyType()) {\n                throw new InvalidProgramException(\n                        \"Return type \"\n                                + keyType\n                                + \" of KeySelector \"\n                                + keyExtractor.getClass()\n                                + \" is not a valid key type\");\n            }\n\n            this.keyExtractor = keyExtractor;\n            this.inputType = inputType;\n            this.keyType = keyType;\n\n            this.originalKeyTypes = new TypeInformation[] {keyType};\n            if (keyType instanceof CompositeType) {\n                this.keyFields =\n                        ((CompositeType<T>) keyType).getFlatFields(ExpressionKeys.SELECT_ALL_CHAR);","sourceCodeStart":81,"sourceCodeEnd":117,"githubUrl":"https://github.com/apache/flink/blob/2f3c205e9266cb30240eb7f4fdab15cad629a70f/flink-core/src/main/java/org/apache/flink/api/common/operators/Keys.java#L81-L117","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nnew SelectorFunctionKeys<>(sel, inType, null);\n// after\nnew SelectorFunctionKeys<>(sel, inType, Types.STRING);","handlingStrategy":"validation","validationCode":"TypeInformation<K> kt = Objects.requireNonNull(keyType, \"keyType\");\nnew SelectorFunctionKeys<>(keySelector, inputType, kt);","typeGuard":"static <K> boolean hasKeyType(TypeInformation<K> t) { return t != null; }","tryCatchPattern":null,"preventionTips":["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."],"tags":["flink-core","keys","type-information","null-check","key-selector"],"backgroundTag":null,"analyzedSha":"2f3c205e9266cb30240eb7f4fdab15cad629a70f","analyzedAt":"2026-08-14T08:48:24.518Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}