{"id":"1ab636af12cb74e4","repo":"google/gson","slug":"value-null","errorCode":null,"errorMessage":"value == null","messagePattern":"value == null","errorType":"exception","errorClass":"NullPointerException","httpStatus":null,"severity":"error","filePath":"gson/src/main/java/com/google/gson/internal/LinkedTreeMap.java","lineNumber":112,"sourceCode":"  @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;\n    header.next = header.prev = header;\n  }\n","sourceCodeStart":94,"sourceCodeEnd":130,"githubUrl":"https://github.com/google/gson/blob/8b8628c65699bc4421696183c62ae0c1b9b281dc/gson/src/main/java/com/google/gson/internal/LinkedTreeMap.java#L94-L130","documentation":"Thrown by LinkedTreeMap.put(K, V) (NullPointerException 'value == null') when allowNullValues is false. The default no-arg constructor enables null values, but the constructor LinkedTreeMap(boolean allowNullValues) or LinkedTreeMap(comparator, false) disables them, so putting a null value violates the map's contract and is rejected immediately.","triggerScenarios":"Constructing LinkedTreeMap with allowNullValues=false (directly or via LinkedTreeMap(false)) and calling put(key, null). Also reached when Gson builds a LinkedTreeMap with null values disallowed and the JSON omits a value or contains an explicit null for an entry value while the target type forbids nulls.","commonSituations":"Gson configured to serialize nulls (serializeNulls()) deserializing into a type that uses a null-disallowing tree map; explicitly constructing LinkedTreeMap(false) to harden a map and then inserting null; merging partial data with missing fields.","solutions":["Construct the map allowing null values if nulls are expected: new LinkedTreeMap<>(true) or the no-arg constructor.","Default missing/null values to a non-null placeholder before insertion (e.g. empty string, 0, Optional.empty).","Configure Gson not to serialize nulls so omitted fields are skipped rather than written as null."],"exampleFix":"// before\nLinkedTreeMap<String, String> m = new LinkedTreeMap<>(false);\nm.put(\"k\", null); // throws\n\n// after: allow null values, or supply a default\nLinkedTreeMap<String, String> m = new LinkedTreeMap<>(true);\nm.put(\"k\", null); // ok","handlingStrategy":"validation","validationCode":"LinkedTreeMap<String, String> map = new LinkedTreeMap<>(true); // allow null values\n// or guard each put:\nstatic <V> void putSafeValue(LinkedTreeMap<String, V> m, String k, V v, V fallback) {\n  m.put(k, v != null ? v : fallback);\n}","typeGuard":"null","tryCatchPattern":"try {\n  map.put(k, v);\n} catch (NullPointerException e) {\n  if (e.getMessage().contains(\"value == null\")) {\n    map.put(k, defaultValue);\n  } else throw e;\n}","preventionTips":["Construct LinkedTreeMap with allowNullValues=true when nulls are valid.","Default missing fields to non-null placeholders.","Avoid serializeNulls() when the target map forbids null values."],"tags":["gson","collection","linkedtreemap","null","map"],"analyzedSha":"8b8628c65699bc4421696183c62ae0c1b9b281dc","analyzedAt":"2026-08-04T19:12:22.202Z","schemaVersion":2}