{"record":{"id":"1b351249f28837bd","repo":"apache/hadoop","slug":"null-element-is-not-supported","errorCode":null,"errorMessage":"Null element is not supported.","messagePattern":"Null element is not supported\\.","errorType":"exception","errorClass":"NullPointerException","httpStatus":null,"severity":"error","filePath":"hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/LightWeightGSet.java","lineNumber":153,"sourceCode":"    for(LinkedElement e = entries[index]; e != null; e = e.getNext()) {\n      if (e.equals(key)) {\n        return convert(e);\n      }\n    }\n    //element not found\n    return null;\n  }\n\n  @Override\n  public boolean contains(final K key) {\n    return get(key) != null;\n  }\n\n  @Override\n  public E put(final E element) {\n    // validate element\n    if (element == null) {\n      throw new NullPointerException(\"Null element is not supported.\");\n    }\n    LinkedElement e = null;\n    try {\n      e = (LinkedElement)element;\n    } catch (ClassCastException ex) {\n      throw new HadoopIllegalArgumentException(\n          \"!(element instanceof LinkedElement), element.getClass()=\"\n          + element.getClass());\n    }\n\n    // find index\n    final int index = getIndex(element);\n\n    // remove if it already exists\n    final E existing = remove(index, element);\n\n    // insert the element to the head of the linked list\n    modification++;","sourceCodeStart":135,"sourceCodeEnd":171,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/LightWeightGSet.java#L135-L171","documentation":"put(E) rejects null elements with NullPointerException(\"Null element is not supported.\"). The set chains elements itself through LinkedElement, so a null entry has no representation; the check at the top of put() is the cheapest place to catch the bug.","triggerScenarios":"Calling gset.put(null) - typically a create-then-insert flow where the factory or lookup returned null (not found, allocation failure) and the result was inserted unchecked.","commonSituations":"Cache fill code doing gset.put(load(x)) where load returns null on miss; refactoring that removed a null branch; tests inserting mock nulls.","solutions":["Null-check the element between creation and insertion.","Make 'absent' explicit at the creator (Optional or a not-found flag) instead of returning null.","Use Objects.requireNonNull(element, \"element from <source>\") to fail with context."],"exampleFix":"// before\ngset.put(createEntry(id)); // createEntry returns null on failure\n\n// after\nE e = createEntry(id);\nif (e != null) {\n  gset.put(e);\n}","handlingStrategy":"validation","validationCode":"E element = factory.create(id);\nif (element == null) {\n  // handle absence explicitly; do not insert\n  return;\n}\ngset.put(element);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Do not insert factory results without a null check.","Reserve null for 'absent' in lookups only, never for insert arguments."],"tags":["collections","null-safety","hadoop-common"],"backgroundTag":"null-argument","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-22T20:17:22.307Z"}