{"record":{"id":"dae3acd923234872","repo":"apache/hadoop","slug":"key-can-not-be-null-dae3ac","errorCode":null,"errorMessage":"key can not be null","messagePattern":"key can not be null","errorType":"validation","errorClass":"NullPointerException","httpStatus":null,"severity":"error","filePath":"hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/bloom/RetouchedBloomFilter.java","lineNumber":121,"sourceCode":"  \n  /**\n   * Constructor\n   * @param vectorSize The vector size of <i>this</i> filter.\n   * @param nbHash The number of hash function to consider.\n   * @param hashType type of the hashing function (see\n   * {@link org.apache.hadoop.util.hash.Hash}).\n   */\n  public RetouchedBloomFilter(int vectorSize, int nbHash, int hashType) {\n    super(vectorSize, nbHash, hashType);\n\n    this.rand = null;\n    createVector();\n  }\n\n  @Override\n  public void add(Key key) {\n    if (key == null) {\n      throw new NullPointerException(\"key can not be null\");\n    }\n\n    int[] h = hash.hash(key);\n    hash.clear();\n\n    for (int i = 0; i < nbHash; i++) {\n      bits.set(h[i]);\n      keyVector[h[i]].add(key);\n    }\n  }\n\n  /**\n   * Adds a false positive information to <i>this</i> retouched Bloom filter.\n   * <p>\n   * <b>Invariant</b>: if the false positive is <code>null</code>, nothing happens.\n   * @param key The false positive key to add.\n   */\n  public void addFalsePositive(Key key) {","sourceCodeStart":103,"sourceCodeEnd":139,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/bloom/RetouchedBloomFilter.java#L103-L139","documentation":"RetouchedBloomFilter.add(Key) both sets bits and appends the key to the per-bit keyVector that later retouching (selectiveClearing) walks. It throws NullPointerException for a null key before hashing because both operations need the key object.","triggerScenarios":"rbf.add(null); batch loops iterating a collection that contains null elements; passing a Key variable that an upstream branch never assigned.","commonSituations":"Ingestion loops over nullable records; tests using Arrays.asList(null, key); code that uses null as a 'no key' sentinel.","solutions":["Strip nulls from the input before the loop: keys.removeIf(Objects::isNull)","Guard each element: if (k != null) rbf.add(k)","Fix the producer so null keys never reach the filter"],"exampleFix":"// before\nfor (Key k : keys) {\n  rbf.add(k); // NPE when k == null\n}\n\n// after\nfor (Key k : keys) {\n  if (k != null) {\n    rbf.add(k);\n  }\n}","handlingStrategy":"validation","validationCode":"for (Key k : keys) {\n  if (k != null) {\n    rbf.add(k);\n  }\n}","typeGuard":"static boolean isAddableKey(Key k) {\n  return k != null && k.getBytes() != null;\n}","tryCatchPattern":null,"preventionTips":["Remove nulls once at the boundary: keys.removeIf(Objects::isNull)","Use Objects.requireNonNull(key) in your own wrapper so failures point at your layer, not the filter","Do not overload null with control-flow meaning (end-of-input) in filter feeds"],"tags":["hadoop","bloom-filter","java","null-check"],"backgroundTag":"null-argument-exception","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-23T01:17:44.959Z"}