{"record":{"id":"150114207e8209e7","repo":"google/gson","slug":"please-begin-an-object-before-writing-a-name","errorCode":null,"errorMessage":"Please begin an object before writing a name.","messagePattern":"Please begin an object before writing a name\\.","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"gson/src/main/java/com/google/gson/internal/bind/JsonTreeWriter.java","lineNumber":157,"sourceCode":"      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  }\n\n  @CanIgnoreReturnValue\n  @Override\n  public JsonWriter value(boolean value) throws IOException {\n    put(new JsonPrimitive(value));\n    return this;\n  }","sourceCodeStart":139,"sourceCodeEnd":175,"githubUrl":"https://github.com/google/gson/blob/310ac341f2f92a454b229bf21f70d2d18b2b6db7/gson/src/main/java/com/google/gson/internal/bind/JsonTreeWriter.java#L139-L175","documentation":"JsonTreeWriter.name() throws IllegalStateException(\"Please begin an object before writing a name.\") when the current open container is an array, not an object. JSON member names (keys) are only valid inside JSON objects; arrays contain positional values only. Calling name() inside an array is a structural error.","triggerScenarios":"Calling name() after beginArray() instead of beginObject(); misidentifying the current container type in a custom adapter; code that assumes an object context but opened an array.","commonSituations":"Refactor that swapped container types; adapter logic that branches on the wrong container; copy-paste of keyed emission into array code.","solutions":["Call name() only inside an object (beginObject), never inside an array (beginArray)","If key-value pairs are needed, open with beginObject() rather than beginArray()","Track and check the current container type before emitting a name"],"exampleFix":"// before - name inside an array\nwriter.beginArray();\nwriter.name(\"x\");\n\n// after - use an object for keyed members\nwriter.beginObject();\nwriter.name(\"x\").value(1);","handlingStrategy":"validation","validationCode":"// Only call name() when the current container is a JsonObject\nif (!(topOfStack instanceof JsonObject)) {\n    throw new IllegalStateException(\"name() requires an object container, not an array\");\n}\nwriter.name(key);","typeGuard":null,"tryCatchPattern":"try {\n  writer.name(key);\n} catch (IllegalStateException e) {\n  // 'Please begin an object...': switch from beginArray() to beginObject(), or drop the name()\n}","preventionTips":["Call name() only inside objects, never inside arrays","Use beginObject() when key-value pairs are needed","Track the current container type before emitting a name"],"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"}