{"id":"4ca8e6281cb5e168","repo":"google/gson","slug":"null-is-not-a-valid-atomiclongarray-element","errorCode":null,"errorMessage":"null is not a valid AtomicLongArray element","messagePattern":"null is not a valid AtomicLongArray element","errorType":"exception","errorClass":"JsonSyntaxException","httpStatus":null,"severity":"error","filePath":"gson/src/main/java/com/google/gson/internal/bind/TypeAdapters.java","lineNumber":384,"sourceCode":"          }\n          out.endArray();\n        }\n      }.nullSafe();\n  public static final TypeAdapterFactory ATOMIC_INTEGER_ARRAY_FACTORY =\n      newFactory(AtomicIntegerArray.class, TypeAdapters.ATOMIC_INTEGER_ARRAY);\n\n  public static TypeAdapter<AtomicLongArray> atomicLongArrayAdapter(\n      TypeAdapter<Number> longAdapter) {\n    Objects.requireNonNull(longAdapter);\n    return new TypeAdapter<AtomicLongArray>() {\n      @Override\n      public AtomicLongArray read(JsonReader in) throws IOException {\n        List<Long> list = new ArrayList<>();\n        in.beginArray();\n        while (in.hasNext()) {\n          Number value = longAdapter.read(in);\n          if (value == null) {\n            throw new JsonSyntaxException(\"null is not a valid AtomicLongArray element\");\n          }\n          list.add(value.longValue());\n        }\n        in.endArray();\n        int length = list.size();\n        AtomicLongArray array = new AtomicLongArray(length);\n        for (int i = 0; i < length; ++i) {\n          array.set(i, list.get(i));\n        }\n        return array;\n      }\n\n      @Override\n      public void write(JsonWriter out, AtomicLongArray value) throws IOException {\n        out.beginArray();\n        for (int i = 0, length = value.length(); i < length; i++) {\n          longAdapter.write(out, value.get(i));\n        }","sourceCodeStart":366,"sourceCodeEnd":402,"githubUrl":"https://github.com/google/gson/blob/8b8628c65699bc4421696183c62ae0c1b9b281dc/gson/src/main/java/com/google/gson/internal/bind/TypeAdapters.java#L366-L402","documentation":"The AtomicLongArray adapter reads elements using a delegated long TypeAdapter; if that adapter returns null for any element (e.g., the JSON contains a null inside the array and the long adapter is not nullSafe), Gson throws JsonSyntaxException. The AtomicLongArray cannot hold null longs, so a null element is unrecoverable.","triggerScenarios":"Deserialifying JSON like [1, null, 3] into an AtomicLongArray; using a custom long TypeAdapter that returns null on certain tokens; the default LONG adapter returns null only on JSON NULL token. Triggered at line 384 when value==null.","commonSituations":"Sparse arrays with null placeholders; adapters that map missing/unknown tokens to null; protocols that emit null for absent readings.","solutions":["Remove or replace null elements in the source JSON before deserialization.","Register a custom TypeAdapter<Number> for the long adapter that maps null to 0L.","Change the target type to List<Long> (or AtomicReferenceArray<Long>) if nulls are meaningful.","Pre-validate the array shape: fail fast at the trust boundary if nulls are not allowed."],"exampleFix":"// before\nAtomicLongArray a = gson.fromJson(\"[1,null,3]\", AtomicLongArray.class); // throws\n\n// after\nJsonArray arr = JsonParser.parseString(json).getAsJsonArray();\nlong[] vals = new long[arr.size()];\nfor (int i=0;i<arr.size();i++) vals[i] = arr.get(i).isJsonNull() ? 0L : arr.get(i).getAsLong();\nAtomicLongArray a = new AtomicLongArray(vals);","handlingStrategy":"validation","validationCode":"// Reject null elements before deserializing into AtomicLongArray\nfor (JsonElement e : arr) {\n  if (e.isJsonNull()) throw new IllegalArgumentException(\"null AtomicLongArray element\");\n}","typeGuard":"null","tryCatchPattern":"try {\n  gson.fromJson(json, AtomicLongArray.class);\n} catch (JsonSyntaxException e) {\n  if (e.getMessage().equals(\"null is not a valid AtomicLongArray element\")) {\n    // strip nulls and retry, or change target type\n  } else throw e;\n}","preventionTips":["Reject sparse/null arrays at the trust boundary.","Register a custom long adapter that maps null to 0L.","Use List<Long> when nulls are meaningful."],"tags":["gson","atomiclongarray","deserialization","null","number"],"analyzedSha":"8b8628c65699bc4421696183c62ae0c1b9b281dc","analyzedAt":"2026-08-04T19:12:22.202Z","schemaVersion":2}