{"record":{"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":386,"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":368,"sourceCodeEnd":404,"githubUrl":"https://github.com/google/gson/blob/310ac341f2f92a454b229bf21f70d2d18b2b6db7/gson/src/main/java/com/google/gson/internal/bind/TypeAdapters.java#L368-L404","documentation":"The AtomicLongArray adapter reads each element via a long adapter. If the long adapter returns null (because the JSON value is null), Gson cannot store it as a primitive long in the AtomicLongArray and throws JsonSyntaxException.","triggerScenarios":"Deserializing a JSON array containing a null literal into an AtomicLongArray field, e.g. [1, null, 3].","commonSituations":"An API returns nulls inside a numeric array; schema mismatch where the field type does not match nullable source data.","solutions":["Pre-process the JSON array to replace null elements with 0 or another default before deserializing","Register a custom TypeAdapter<AtomicLongArray> that maps null elements to 0","Fix the source data to omit or replace nulls in the array"],"exampleFix":"// before\ngson.fromJson(\"[1, null, 3]\", AtomicLongArray.class); // throws on null\n\n// after — filter nulls from the array\nJsonArray arr = JsonParser.parseString(\"[1, null, 3]\").getAsJsonArray();\nfor (int i = 0; i < arr.size(); i++) {\n    if (arr.get(i).isJsonNull()) arr.set(i, new JsonPrimitive(0L));\n}\nAtomicLongArray result = gson.fromJson(arr, AtomicLongArray.class);","handlingStrategy":"validation","validationCode":"// Validate that no element in the array is null before deserializing into AtomicLongArray\npublic static JsonArray sanitizeLongArray(JsonArray arr) {\n    for (int i = 0; i < arr.size(); i++) {\n        if (arr.get(i).isJsonNull()) {\n            arr.set(i, new JsonPrimitive(0L)); // replace null with default\n        }\n    }\n    return arr;\n}","typeGuard":null,"tryCatchPattern":"try {\n    AtomicLongArray arr = gson.fromJson(json, AtomicLongArray.class);\n} catch (JsonSyntaxException e) {\n    if (e.getMessage().contains(\"null is not a valid AtomicLongArray element\")) {\n        // sanitize array to replace nulls with 0 before retrying\n    }\n}","preventionTips":["Pre-process JSON arrays to replace null elements with 0 before deserializing into AtomicLongArray","Register a custom TypeAdapter<AtomicLongArray> that handles null elements gracefully","Validate source data does not contain nulls in numeric arrays at the API boundary"],"tags":["deserialization","atomiclongarray","null","numeric"],"backgroundTag":null,"analyzedSha":"310ac341f2f92a454b229bf21f70d2d18b2b6db7","analyzedAt":"2026-08-10T02:58:47.455Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}