{"id":"a7c483b0a9761c85","repo":"google/gson","slug":"already-wrote-a-name-expecting-a-value","errorCode":null,"errorMessage":"Already wrote a name, expecting a value.","messagePattern":"Already wrote a name, expecting a value\\.","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"gson/src/main/java/com/google/gson/stream/JsonWriter.java","lineNumber":502,"sourceCode":"    return stack[stackSize - 1];\n  }\n\n  /** Replace the value on the top of the stack with the given value. */\n  private void replaceTop(int topOfStack) {\n    stack[stackSize - 1] = topOfStack;\n  }\n\n  /**\n   * Encodes the property name.\n   *\n   * @param name the name of the forthcoming value. May not be {@code null}.\n   * @return this writer.\n   */\n  @CanIgnoreReturnValue\n  public JsonWriter name(String name) throws IOException {\n    Objects.requireNonNull(name, \"name == null\");\n    if (deferredName != null) {\n      throw new IllegalStateException(\"Already wrote a name, expecting a value.\");\n    }\n    int context = peek();\n    if (context != EMPTY_OBJECT && context != NONEMPTY_OBJECT) {\n      throw new IllegalStateException(\"Please begin an object before writing a name.\");\n    }\n    deferredName = name;\n    return this;\n  }\n\n  private void writeDeferredName() throws IOException {\n    if (deferredName != null) {\n      beforeName();\n      string(deferredName);\n      deferredName = null;\n    }\n  }\n\n  /**","sourceCodeStart":484,"sourceCodeEnd":520,"githubUrl":"https://github.com/google/gson/blob/8b8628c65699bc4421696183c62ae0c1b9b281dc/gson/src/main/java/com/google/gson/stream/JsonWriter.java#L484-L520","documentation":"Thrown by JsonWriter.name() when deferredName is already non-null, meaning a previous name() was called but no value has been written yet. JSON objects require strictly alternating name/value pairs; two names in a row is invalid and would produce malformed output.","triggerScenarios":"Calling writer.name(\"a\").name(\"b\") without a value between them, or any logic where name() is invoked twice because a value write was conditionally skipped or reordered.","commonSituations":"Loop-based serialization where the name is written at the top of the loop but the value is sometimes skipped; refactoring that moves name() out of an if branch; copy-paste errors adding a duplicate name() call.","solutions":["Always pair name() with exactly one value write before the next name().","Move name() inside the conditional so it is only emitted alongside its value.","Track write state explicitly if using loops that may skip values.","Use Gson's POJO serialization instead of manual name()/value() calls to avoid this class of bug."],"exampleFix":"// before\nwriter.name(\"a\");\nif (cond) writer.value(1);\nwriter.name(\"b\"); // throws if cond was false: 'Already wrote a name'\n\n// after\nif (cond) writer.name(\"a\").value(1);\nwriter.name(\"b\").value(2);","handlingStrategy":"validation","validationCode":"// Track whether a value is pending before calling name() again\nboolean nameWritten = false;\n// always:\nif (!nameWritten) {\n  writer.name(\"x\").value(v);\n  // or set nameWritten=false after value\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always follow name() with exactly one value (or nested begin) before the next name().","Keep name() and value() adjacent in code; never separate them with control flow that may skip the value.","Use POJO serialization to avoid manual name/value bookkeeping.","Add tests covering both branches of conditional field serialization."],"tags":["gson","jsonwriter","state-machine","api-misuse"],"analyzedSha":"8b8628c65699bc4421696183c62ae0c1b9b281dc","analyzedAt":"2026-08-04T19:12:22.202Z","schemaVersion":2}