{"record":{"id":"c5bed3c5cbd0dcc7","repo":"google/gson","slug":"key-null","errorCode":null,"errorMessage":"key == null","messagePattern":"key == null","errorType":"exception","errorClass":"NullPointerException","httpStatus":null,"severity":"error","filePath":"gson/src/main/java/com/google/gson/internal/LinkedTreeMap.java","lineNumber":109,"sourceCode":"    return size;\n  }\n\n  @Override\n  public V get(Object key) {\n    Node<K, V> node = findByObject(key);\n    return node != null ? node.value : null;\n  }\n\n  @Override\n  public boolean containsKey(Object key) {\n    return findByObject(key) != null;\n  }\n\n  @CanIgnoreReturnValue\n  @Override\n  public V put(K key, V value) {\n    if (key == null) {\n      throw new NullPointerException(\"key == null\");\n    }\n    if (value == null && !allowNullValues) {\n      throw new NullPointerException(\"value == null\");\n    }\n    Node<K, V> created = find(key, true);\n    V result = created.value;\n    created.value = value;\n    return result;\n  }\n\n  @Override\n  public void clear() {\n    root = null;\n    size = 0;\n    modCount++;\n\n    // Clear iteration order\n    Node<K, V> header = this.header;","sourceCodeStart":91,"sourceCodeEnd":127,"githubUrl":"https://github.com/google/gson/blob/310ac341f2f92a454b229bf21f70d2d18b2b6db7/gson/src/main/java/com/google/gson/internal/LinkedTreeMap.java#L91-L127","documentation":"LinkedTreeMap is a red-black tree keyed by Comparable keys; null is not a valid key (no comparison is possible), so put throws NullPointerException on a null key (LinkedTreeMap.java:108). This mirrors TreeMap's null-key rejection in natural-ordering mode.","triggerScenarios":"Calling LinkedTreeMap.put(null, value), or any Map.put/computeIfAbsent/merge style call that supplies null as the key, on a LinkedTreeMap instance.","commonSituations":"Gson's internal JsonObject-like structures backed by LinkedTreeMap when a JSON field name resolves to null (rare for JSON, common with programmatic map construction); generic Map<K,V> code that assumes HashMap-style null keys are tolerated.","solutions":["Guard the caller: skip, replace, or reject null keys before calling put.","Use a HashMap/LinkedHashMap if you genuinely need null keys.","When building maps from untrusted input, normalize keys (e.g. replace null with a sentinel string) at the boundary."],"exampleFix":"// before\nmap.put(maybeNullKey, value); // throws if key null\n// after\nif (maybeNullKey != null) map.put(maybeNullKey, value);","handlingStrategy":"validation","validationCode":"if (key == null) throw new IllegalArgumentException(\"key must not be null\"); map.put(key, value);","typeGuard":"static <K> boolean puttableKey(LinkedTreeMap<K,?> m, K k) { return k != null; }","tryCatchPattern":"try { map.put(key, value); } catch (NullPointerException e) { /* handle null key */ }","preventionTips":["Filter or reject null keys at the boundary that feeds LinkedTreeMap.","If your data legitimately has null keys, use HashMap/LinkedHashMap instead.","Annotate map-typed fields with @NotNull when they back a LinkedTreeMap."],"tags":["gson","collections","null","linkedtreemap"],"backgroundTag":null,"analyzedSha":"310ac341f2f92a454b229bf21f70d2d18b2b6db7","analyzedAt":"2026-08-10T02:58:47.455Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}