google/gson · error · IllegalStateException

Already wrote a name, expecting a value.

Error message

Already wrote a name, expecting a value.

What it means

Thrown by JsonWriter.name() (JsonWriter.java:501-503) when deferredName is already set, i.e. name() was called twice in a row without a value in between. JSON object members are name:value pairs, so two consecutive names would be invalid; the writer stores the name in deferredName and flushes it only when a value (or nullValue) is written.

Solutions

  1. Always write a value (or nullValue()) immediately after each name().
  2. Restructure loops so each iteration emits a complete name/value pair.
  3. Guard name() calls so you never call it twice without an intervening value.
  4. Add a serializer test that detects double-name sequences.

Example fix

// before
writer.beginObject();
for (String k : keys) {
  writer.name(k);
}
writer.endObject(); // second name() throws

// after
writer.beginObject();
for (String k : keys) {
  writer.name(k).value(map.get(k));
}
writer.endObject();
Defensive patterns

Strategy: validation

Validate before calling

writer.beginObject();
for (Map.Entry<String,V> e : map.entrySet()) {
  writer.name(e.getKey()).value(e.getValue());
}
writer.endObject();

Type guard

// Ensure exactly one value follows each name within a single code block.
assert deferredValueEmitted : "name without value";

Try / catch

try {
  writer.name("x").name("y");
} catch (IllegalStateException e) {
  // programming error; fix the serializer, do not swallow
  throw e;
}

Prevention

When it happens

Trigger: Calling writer.name("a").name("b") with no value between them; a loop that emits names without writing values; serializers that conditionally call name() twice on the same field.

Common situations: Loop bodies that always call name() but skip the value on some iterations; copy-paste of a name() call; refactors that moved the value write out of the name() block.

Related errors


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

Appendix: source

Thrown at gson/src/main/java/com/google/gson/stream/JsonWriter.java:502

    return stack[stackSize - 1];
  }

  /** Replace the value on the top of the stack with the given value. */
  private void replaceTop(int topOfStack) {
    stack[stackSize - 1] = topOfStack;
  }

  /**
   * Encodes the property name.
   *
   * @param name the name of the forthcoming value. May not be {@code null}.
   * @return this writer.
   */
  @CanIgnoreReturnValue
  public JsonWriter name(String name) throws IOException {
    Objects.requireNonNull(name, "name == null");
    if (deferredName != null) {
      throw new IllegalStateException("Already wrote a name, expecting a value.");
    }
    int context = peek();
    if (context != EMPTY_OBJECT && context != NONEMPTY_OBJECT) {
      throw new IllegalStateException("Please begin an object before writing a name.");
    }
    deferredName = name;
    return this;
  }

  private void writeDeferredName() throws IOException {
    if (deferredName != null) {
      beforeName();
      string(deferredName);
      deferredName = null;
    }
  }

  /**

View on GitHub (pinned to 310ac341f2)