{"record":{"id":"f2714b34b3253f0c","repo":"apache/hadoop","slug":"key-cannot-be-null","errorCode":null,"errorMessage":"key cannot be null","messagePattern":"key cannot be null","errorType":"exception","errorClass":"NullPointerException","httpStatus":null,"severity":"error","filePath":"hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/bloom/BloomFilter.java","lineNumber":119,"sourceCode":"  }\n  \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 BloomFilter(int vectorSize, int nbHash, int hashType) {\n    super(vectorSize, nbHash, hashType);\n\n    bits = new BitSet(this.vectorSize);\n  }\n\n  @Override\n  public void add(Key key) {\n    if(key == null) {\n      throw new NullPointerException(\"key cannot 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    }\n  }\n\n  @Override\n  public void and(Filter filter) {\n    if(filter == null\n        || !(filter instanceof BloomFilter)\n        || filter.vectorSize != this.vectorSize\n        || filter.nbHash != this.nbHash) {\n      throw new IllegalArgumentException(\"filters cannot be and-ed\");\n    }","sourceCodeStart":101,"sourceCodeEnd":137,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/bloom/BloomFilter.java#L101-L137","documentation":"BloomFilter.add(Key) hashes the key and sets the corresponding bits in its BitSet; a null key cannot be hashed, so the method throws NullPointerException with this message as an explicit precondition check. This is fail-fast validation, not a JVM-generated NPE — the class rejects null up front with a clear message.","triggerScenarios":"bloomFilter.add(null); adding keys from a map lookup that returned null (get on a missing key); stream pipelines feeding nulls into the filter; test code constructing Key arrays with gaps.","commonSituations":"Cache-dedup or previously-seen checks where the key source can be absent; deserialization producing null Keys for missing fields; refactoring that changed a key extractor to return null.","solutions":["Null-check the key before calling add() and skip or reject the record.","Fix the upstream source of nulls (map.get default, Optional, filter(Objects::nonNull)).","Wrap the call in try/catch NullPointerException if you cannot change the producer (last resort)."],"exampleFix":"// before\nbloomFilter.add(keyMap.get(id)); // get(id) returned null\n// throws NullPointerException: key cannot be null\n\n// after\nKey k = keyMap.get(id);\nif (k != null) {\n  bloomFilter.add(k);\n}","handlingStrategy":"validation","validationCode":"if (key == null) {\n  LOG.debug(\"Skipping null key for bloom filter\");\n  return;\n}\nbloomFilter.add(key);","typeGuard":null,"tryCatchPattern":"try {\n  bloomFilter.add(key);\n} catch (NullPointerException e) {\n  // guard against nulls from untrusted producers\n  LOG.warn(\"Rejected null key\", e);\n}","preventionTips":["Filter nulls at ingestion: stream.filter(Objects::nonNull).","Use Optional or containsKey-style checks instead of nullable key lookups.","Wrap external input parsing with a non-null Key factory that throws with context."],"tags":["bloom-filter","null-check","precondition","hadoop-common"],"backgroundTag":"null-argument","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-22T20:17:22.307Z"}