{"record":{"id":"c5297130185db28f","repo":"apache/flink","slug":"key-extractor-must-not-be-null","errorCode":null,"errorMessage":"Key extractor must not be null.","messagePattern":"Key extractor 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":96,"sourceCode":"    // --------------------------------------------------------------------------------------------\n    //  Specializations for expression-based / extractor-based grouping\n    // --------------------------------------------------------------------------------------------\n\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};","sourceCodeStart":78,"sourceCodeEnd":114,"githubUrl":"https://github.com/apache/flink/blob/2f3c205e9266cb30240eb7f4fdab15cad629a70f/flink-core/src/main/java/org/apache/flink/api/common/operators/Keys.java#L78-L114","documentation":"Thrown by the SelectorFunctionKeys constructor when the KeySelector argument is null. A KeySelector defines how each record maps to its key; a null selector would NPE during partitioning, sorting, and joining, so it is rejected up front with a NullPointerException.","triggerScenarios":"Calling a keyBy()/groupBy()/join API with a KeySelector variable that is null; passing a method reference that resolved to null due to a reflection/lambda capture bug; building keys programmatically from an optional that was empty.","commonSituations":"Passing an uninitialized KeySelector field; refactoring a lambda into a variable that was never assigned; framework code that obtains the selector from a registry returning null.","solutions":["Ensure the KeySelector instance is constructed and non-null before passing it.","Use Objects.requireNonNull(selector, \"keySelector\") at the call site for an earlier, clearer failure.","If the selector is conditional, supply a concrete selector (e.g. identity) rather than null."],"exampleFix":"// before\nKeySelector<IN,KEY> sel = registry.get(name);\nds.keyBy(sel);\n// after\nKeySelector<IN,KEY> sel = Objects.requireNonNull(registry.get(name), \"selector for \" + name);\nds.keyBy(sel);","handlingStrategy":"validation","validationCode":"Objects.requireNonNull(keySelector, \"keySelector\");\nnew SelectorFunctionKeys<>(keySelector, inputType, keyType);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always construct the KeySelector before passing it; never forward an uninitialised field.","Use Objects.requireNonNull at the call site for a clearer error.","Avoid optional-returning suppliers for selectors; provide a concrete default."],"tags":["flink-core","keys","key-selector","null-check"],"backgroundTag":null,"analyzedSha":"2f3c205e9266cb30240eb7f4fdab15cad629a70f","analyzedAt":"2026-08-14T08:48:24.518Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}