google/gson · error · IllegalStateException

Did not expect a name

Error message

Did not expect a name

What it means

JsonTreeWriter.name() throws IllegalStateException("Did not expect a name") when the stack is empty (no object open) or when a name is already pending (name() was called but no value followed). A JSON name must appear inside an object and be immediately followed by exactly one value; a second name() without an intervening value, or a name() outside any object, violates the JSON grammar.

Solutions

  1. Call name() only inside beginObject()/endObject() and always follow it with exactly one value
  2. Restructure loops so each name() is paired with a value() in the same iteration
  3. Assert no pending name exists before calling name()

Example fix

// before - two names in a row
writer.name("a");
writer.name("b");

// after - name then value, then next name
writer.name("a").value(1);
writer.name("b").value(2);
Defensive patterns

Strategy: validation

Validate before calling

// Only call name() inside an object and when no name is already pending
if (stack.isEmpty() || pendingName != null) {
    throw new IllegalStateException("name() invalid: not in object, or name already pending");
}
writer.name(key);

Try / catch

try {
  writer.name(key);
} catch (IllegalStateException e) {
  // 'Did not expect a name': ensure you are inside an object and the previous name got a value
}

Prevention

When it happens

Trigger: Calling name() before beginObject(); calling name() twice in a row; calling name() after a previous name() whose value was never written.

Common situations: Loop that emits names without values on some iterations; copy-paste adding an extra name(); conditional that writes name() but skips the value branch.

Related errors


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

Appendix: source

Thrown at gson/src/main/java/com/google/gson/internal/bind/JsonTreeWriter.java:150

  @Override
  public JsonWriter endObject() throws IOException {
    if (stack.isEmpty() || pendingName != null) {
      throw new IllegalStateException();
    }
    JsonElement element = peek();
    if (element instanceof JsonObject) {
      stack.remove(stack.size() - 1);
      return this;
    }
    throw new IllegalStateException();
  }

  @CanIgnoreReturnValue
  @Override
  public JsonWriter name(String name) throws IOException {
    Objects.requireNonNull(name, "name == null");
    if (stack.isEmpty() || pendingName != null) {
      throw new IllegalStateException("Did not expect a name");
    }
    JsonElement element = peek();
    if (element instanceof JsonObject) {
      pendingName = name;
      return this;
    }
    throw new IllegalStateException("Please begin an object before writing a name.");
  }

  @CanIgnoreReturnValue
  @Override
  public JsonWriter value(String value) throws IOException {
    if (value == null) {
      return nullValue();
    }
    put(new JsonPrimitive(value));
    return this;
  }

View on GitHub (pinned to 310ac341f2)