google/gson · error · JsonSyntaxException

null is not a valid AtomicLongArray element

Error message

null is not a valid AtomicLongArray element

What it means

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.

Solutions

  1. Pre-process the JSON array to replace null elements with 0 or another default before deserializing
  2. Register a custom TypeAdapter<AtomicLongArray> that maps null elements to 0
  3. Fix the source data to omit or replace nulls in the array

Example fix

// before
gson.fromJson("[1, null, 3]", AtomicLongArray.class); // throws on null

// after — filter nulls from the array
JsonArray arr = JsonParser.parseString("[1, null, 3]").getAsJsonArray();
for (int i = 0; i < arr.size(); i++) {
    if (arr.get(i).isJsonNull()) arr.set(i, new JsonPrimitive(0L));
}
AtomicLongArray result = gson.fromJson(arr, AtomicLongArray.class);
Defensive patterns

Strategy: validation

Validate before calling

// Validate that no element in the array is null before deserializing into AtomicLongArray
public static JsonArray sanitizeLongArray(JsonArray arr) {
    for (int i = 0; i < arr.size(); i++) {
        if (arr.get(i).isJsonNull()) {
            arr.set(i, new JsonPrimitive(0L)); // replace null with default
        }
    }
    return arr;
}

Try / catch

try {
    AtomicLongArray arr = gson.fromJson(json, AtomicLongArray.class);
} catch (JsonSyntaxException e) {
    if (e.getMessage().contains("null is not a valid AtomicLongArray element")) {
        // sanitize array to replace nulls with 0 before retrying
    }
}

Prevention

When it happens

Trigger: Deserializing a JSON array containing a null literal into an AtomicLongArray field, e.g. [1, null, 3].

Common situations: An API returns nulls inside a numeric array; schema mismatch where the field type does not match nullable source data.

Related errors


AI-assisted analysis of google/gson@310ac341f2 (2026-08-10). Data as JSON: /api/errors/4ca8e6281cb5e168. Report an issue: GitHub.

Appendix: source

Thrown at gson/src/main/java/com/google/gson/internal/bind/TypeAdapters.java:386

          }
          out.endArray();
        }
      }.nullSafe();
  public static final TypeAdapterFactory ATOMIC_INTEGER_ARRAY_FACTORY =
      newFactory(AtomicIntegerArray.class, TypeAdapters.ATOMIC_INTEGER_ARRAY);

  public static TypeAdapter<AtomicLongArray> atomicLongArrayAdapter(
      TypeAdapter<Number> longAdapter) {
    Objects.requireNonNull(longAdapter);
    return new TypeAdapter<AtomicLongArray>() {
      @Override
      public AtomicLongArray read(JsonReader in) throws IOException {
        List<Long> list = new ArrayList<>();
        in.beginArray();
        while (in.hasNext()) {
          Number value = longAdapter.read(in);
          if (value == null) {
            throw new JsonSyntaxException("null is not a valid AtomicLongArray element");
          }
          list.add(value.longValue());
        }
        in.endArray();
        int length = list.size();
        AtomicLongArray array = new AtomicLongArray(length);
        for (int i = 0; i < length; ++i) {
          array.set(i, list.get(i));
        }
        return array;
      }

      @Override
      public void write(JsonWriter out, AtomicLongArray value) throws IOException {
        out.beginArray();
        for (int i = 0, length = value.length(); i < length; i++) {
          longAdapter.write(out, value.get(i));
        }

View on GitHub (pinned to 310ac341f2)