{"record":{"id":"239e9240286bbd15","repo":"google/gson","slug":"did-not-expect-a-name","errorCode":null,"errorMessage":"Did not expect a name","messagePattern":"Did not expect a name","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"gson/src/main/java/com/google/gson/internal/bind/JsonTreeWriter.java","lineNumber":150,"sourceCode":"  @Override\n  public JsonWriter endObject() throws IOException {\n    if (stack.isEmpty() || pendingName != null) {\n      throw new IllegalStateException();\n    }\n    JsonElement element = peek();\n    if (element instanceof JsonObject) {\n      stack.remove(stack.size() - 1);\n      return this;\n    }\n    throw new IllegalStateException();\n  }\n\n  @CanIgnoreReturnValue\n  @Override\n  public JsonWriter name(String name) throws IOException {\n    Objects.requireNonNull(name, \"name == null\");\n    if (stack.isEmpty() || pendingName != null) {\n      throw new IllegalStateException(\"Did not expect a name\");\n    }\n    JsonElement element = peek();\n    if (element instanceof JsonObject) {\n      pendingName = name;\n      return this;\n    }\n    throw new IllegalStateException(\"Please begin an object before writing a name.\");\n  }\n\n  @CanIgnoreReturnValue\n  @Override\n  public JsonWriter value(String value) throws IOException {\n    if (value == null) {\n      return nullValue();\n    }\n    put(new JsonPrimitive(value));\n    return this;\n  }","sourceCodeStart":132,"sourceCodeEnd":168,"githubUrl":"https://github.com/google/gson/blob/310ac341f2f92a454b229bf21f70d2d18b2b6db7/gson/src/main/java/com/google/gson/internal/bind/JsonTreeWriter.java#L132-L168","documentation":"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.","triggerScenarios":"Calling name() before beginObject(); calling name() twice in a row; calling name() after a previous name() whose value was never written.","commonSituations":"Loop that emits names without values on some iterations; copy-paste adding an extra name(); conditional that writes name() but skips the value branch.","solutions":["Call name() only inside beginObject()/endObject() and always follow it with exactly one value","Restructure loops so each name() is paired with a value() in the same iteration","Assert no pending name exists before calling name()"],"exampleFix":"// before - two names in a row\nwriter.name(\"a\");\nwriter.name(\"b\");\n\n// after - name then value, then next name\nwriter.name(\"a\").value(1);\nwriter.name(\"b\").value(2);","handlingStrategy":"validation","validationCode":"// Only call name() inside an object and when no name is already pending\nif (stack.isEmpty() || pendingName != null) {\n    throw new IllegalStateException(\"name() invalid: not in object, or name already pending\");\n}\nwriter.name(key);","typeGuard":null,"tryCatchPattern":"try {\n  writer.name(key);\n} catch (IllegalStateException e) {\n  // 'Did not expect a name': ensure you are inside an object and the previous name got a value\n}","preventionTips":["Call name() only inside beginObject()/endObject()","Always follow name() with exactly one value before the next name()","In loops, pair name() and value() within the same iteration"],"tags":["json","serialization","tree-model","state-machine"],"backgroundTag":null,"analyzedSha":"310ac341f2f92a454b229bf21f70d2d18b2b6db7","analyzedAt":"2026-08-10T02:58:47.455Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}