{"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/8b8628c65699bc4421696183c62ae0c1b9b281dc/gson/src/main/java/com/google/gson/internal/LinkedTreeMap.java#L91-L127","documentation":"Thrown by LinkedTreeMap.put(K, V) (NullPointerException 'key == null') because LinkedTreeMap is a red-black tree keyed by Comparable keys and does not support null keys. Unlike HashMap/LinkedHashMap, null cannot be ordered, so the map rejects it outright rather than guessing a position.","triggerScenarios":"Calling map.put(null, value) on a LinkedTreeMap instance. LinkedTreeMap is used internally by Gson when deserializing to a map whose keys must be sorted/compared; a null key in the JSON or in a manually populated LinkedTreeMap triggers it.","commonSituations":"JSON with a null key being deserialized into a SortedMap/TreeMap-backed type; configuring Gson with a type that resolves to LinkedTreeMap and feeding null keys; copying entries from another map without filtering nulls.","solutions":["Filter null keys before inserting: stream/map entries and skip key==null, or substitute a sentinel non-null key.","If null keys are legitimate, use a LinkedHashMap or HashMap instead of a tree-backed map; configure your Gson type target accordingly (avoid TreeMap/SortedMap for the field type).","Validate JSON input and reject or map null keys upstream of Gson."],"exampleFix":"// before\nLinkedTreeMap<String, Integer> m = new LinkedTreeMap<>();\nm.put(null, 1); // throws\n\n// after: filter null keys, or use HashMap\nMap<String, Integer> m = new HashMap<>();\nm.put(null, 1); // ok","handlingStrategy":"validation","validationCode":"static <V> void putSafe(Map<String, V> map, String key, V value) {\n  if (key == null) return; // or throw IllegalArgumentException upstream\n  map.put(key, value);\n}","typeGuard":"static boolean isNonNullOrderingCapableKey(Object key) {\n  return key != null;\n}","tryCatchPattern":"try {\n  map.put(key, value);\n} catch (NullPointerException e) {\n  if (e.getMessage().contains(\"key == null\")) {\n    // log and skip, or fall back to a HashMap\n    fallback.put(key, value);\n  } else throw e;\n}","preventionTips":["Filter null keys before populating tree-backed maps.","Prefer HashMap/LinkedHashMap when null keys are possible.","Validate JSON keys are non-null before deserializing into SortedMap types."],"tags":["gson","collection","linkedtreemap","null","map"],"analyzedSha":"8b8628c65699bc4421696183c62ae0c1b9b281dc","analyzedAt":"2026-08-04T19:12:22.202Z","schemaVersion":2}