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 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.

Source

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

          }
          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 8b8628c656)

Solutions

  1. Remove or replace null elements in the source JSON before deserialization.
  2. Register a custom TypeAdapter<Number> for the long adapter that maps null to 0L.
  3. Change the target type to List<Long> (or AtomicReferenceArray<Long>) if nulls are meaningful.
  4. Pre-validate the array shape: fail fast at the trust boundary if nulls are not allowed.

Example fix

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

// after
JsonArray arr = JsonParser.parseString(json).getAsJsonArray();
long[] vals = new long[arr.size()];
for (int i=0;i<arr.size();i++) vals[i] = arr.get(i).isJsonNull() ? 0L : arr.get(i).getAsLong();
AtomicLongArray a = new AtomicLongArray(vals);
Defensive patterns

Strategy: validation

Validate before calling

// Reject null elements before deserializing into AtomicLongArray
for (JsonElement e : arr) {
  if (e.isJsonNull()) throw new IllegalArgumentException("null AtomicLongArray element");
}

Type guard

null

Try / catch

try {
  gson.fromJson(json, AtomicLongArray.class);
} catch (JsonSyntaxException e) {
  if (e.getMessage().equals("null is not a valid AtomicLongArray element")) {
    // strip nulls and retry, or change target type
  } else throw e;
}

Prevention

When it happens

Trigger: 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.

Common situations: Sparse arrays with null placeholders; adapters that map missing/unknown tokens to null; protocols that emit null for absent readings.

Related errors


AI-assisted analysis of google/gson@8b8628c656 (2026-08-04). Data as JSON: /data/errors/4ca8e6281cb5e168.json. Report an issue: GitHub.