{"record":{"id":"cdcdbc2d4ea35c1a","repo":"apache/flink","slug":"a-nullpointerexception-occurred-while-accessing-a","errorCode":null,"errorMessage":"A NullPointerException occurred while accessing a key field in a POJO. Most likely, the value grouped/joined on is null. Field name: {}","messagePattern":"A NullPointerException occurred while accessing a key field in a POJO\\. Most likely, the value grouped/joined on is null\\. Field name: (.+?)","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"flink-core/src/main/java/org/apache/flink/api/java/typeutils/runtime/PojoComparator.java","lineNumber":216,"sourceCode":"                    \"This should not happen since we call setAccesssible(true) in the ctor.\"\n                            + \" fields: \"\n                            + field\n                            + \" obj: \"\n                            + object);\n        }\n        return object;\n    }\n\n    @Override\n    public int hash(T value) {\n        int i = 0;\n        int code = 0;\n        for (; i < this.keyFields.length; i++) {\n            code *= TupleComparatorBase.HASH_SALT[i & 0x1F];\n            try {\n                code += this.comparators[i].hash(accessField(keyFields[i], value));\n            } catch (NullPointerException npe) {\n                throw new RuntimeException(\n                        \"A NullPointerException occurred while accessing a key field in a POJO. \"\n                                + \"Most likely, the value grouped/joined on is null. Field name: \"\n                                + keyFields[i].getName(),\n                        npe);\n            }\n        }\n        return code;\n    }\n\n    @Override\n    public void setReference(T toCompare) {\n        int i = 0;\n        for (; i < this.keyFields.length; i++) {\n            this.comparators[i].setReference(accessField(keyFields[i], toCompare));\n        }\n    }\n\n    @Override","sourceCodeStart":198,"sourceCodeEnd":234,"githubUrl":"https://github.com/apache/flink/blob/2f3c205e9266cb30240eb7f4fdab15cad629a70f/flink-core/src/main/java/org/apache/flink/api/java/typeutils/runtime/PojoComparator.java#L198-L234","documentation":"Thrown by PojoComparator.hash() when computing the hash of a key field raises NullPointerException: this happens when the key FIELD value is null (the field itself was read, but hashing null via the field comparator, or the access chain, NPEs). The message names the exact field whose value is null.","triggerScenarios":"keyBy/groupBy on a POJO field that is null in some records: code *= HASH_SALT; comparators[i].hash(accessField(...)) throws NPE on the null field value, and PojoComparator rethrows with the field name.","commonSituations":"Optional POJO fields used as keys without null filtering; upstream services emitting partially-filled records; schema evolution leaving new key fields null in restored state; joining on a nullable column.","solutions":["Do not key on nullable fields - map nulls to a sentinel value before keyBy (e.g. Optional.map(...).orElse(DEFAULT))","Filter or side-output records with null keys before the keyBy/groupBy operator","Read the field name in the message to pinpoint which key column is null"],"exampleFix":"// before\nd.keyBy(pojo -> pojo.getRegion()) // region may be null\n\n// after\nd.map(p -> p.getRegion() == null ? \"__unknown__\" : p.getRegion())\n .keyBy(r -> r);","handlingStrategy":"validation","validationCode":"// ensure key field is non-null before keyBy\nif (pojo.getKeyField() == null) {\n    sideOutputOrDefault(pojo); // route away or fill sentinel\n}","typeGuard":"static boolean hasNonNullKey(MyPojo p) {\n    return p != null && p.getKeyField() != null;\n}","tryCatchPattern":"try {\n    keyedStream = stream.keyBy(keySelector);\n} // hash() runs later inside the operator; catch at operator level:\ncatch (RuntimeException e) {\n    if (e.getMessage() != null && e.getMessage().contains(\"grouped/joined on is null\")) {\n        // null key field detected - fix upstream data quality\n    }\n    throw e;\n}","preventionTips":["Choose non-nullable fields as keys","Map null key values to sentinels before keyBy","Use the field name in the error message to locate the offending column"],"tags":["pojo","comparator","null-key","hashing"],"backgroundTag":null,"analyzedSha":"2f3c205e9266cb30240eb7f4fdab15cad629a70f","analyzedAt":"2026-08-14T08:48:24.518Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}