{"id":"7dc22e6f035ca41b","repo":"google/gson","slug":"jsonwriter-is-closed","errorCode":null,"errorMessage":"JsonWriter is closed.","messagePattern":"JsonWriter is closed\\.","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"gson/src/main/java/com/google/gson/stream/JsonWriter.java","lineNumber":482,"sourceCode":"    stackSize--;\n    if (context == nonempty) {\n      newline();\n    }\n    out.write(closeBracket);\n    return this;\n  }\n\n  private void push(int newTop) {\n    if (stackSize == stack.length) {\n      stack = Arrays.copyOf(stack, stackSize * 2);\n    }\n    stack[stackSize++] = newTop;\n  }\n\n  /** Returns the value on the top of the stack. */\n  private int peek() {\n    if (stackSize == 0) {\n      throw new IllegalStateException(\"JsonWriter is closed.\");\n    }\n    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\");","sourceCodeStart":464,"sourceCodeEnd":500,"githubUrl":"https://github.com/google/gson/blob/8b8628c65699bc4421696183c62ae0c1b9b281dc/gson/src/main/java/com/google/gson/stream/JsonWriter.java#L464-L500","documentation":"Thrown by JsonWriter.peek() (and flush()) when stackSize == 0, which means close() has already been called and the writer is no longer usable. Any subsequent operation that needs the current scope throws IllegalStateException. The writer is single-use by design.","triggerScenarios":"Calling any write/flush method on a JsonWriter after close(); e.g. a try-with-resources block that closes the writer, followed by code in a finally or outer block that attempts to write more.","commonSituations":"Wrapping JsonWriter in try-with-resources and then writing outside the block; closing the underlying Writer/OutputStream separately and then touching the JsonWriter; flushing in a finally that runs after close() on an error path.","solutions":["Do all writing before close(); structure code so close() is the last operation.","Avoid closing the underlying Writer/OutputStream independently; let JsonWriter.close() manage it.","If you need to reopen, create a new JsonWriter over a fresh destination.","Remove redundant flush() calls in finally blocks that may execute after close()."],"exampleFix":"// before\ntry (JsonWriter w = new JsonWriter(new StringWriter())) {\n  w.beginObject();\n}\nw.name(\"x\"); // throws 'JsonWriter is closed.'\n\n// after\nJsonWriter w = new JsonWriter(new StringWriter());\nw.beginObject();\nw.name(\"x\").value(1);\nw.endObject();\nw.close();","handlingStrategy":"try-catch","validationCode":"// Manage lifecycle explicitly; do not write after close\nJsonWriter writer = new JsonWriter(dest);\ntry {\n  // all writes here\n  writer.beginObject(); writer.endObject();\n} finally {\n  writer.close();\n}\n// set a flag if needed:\nboolean closed = false;","typeGuard":null,"tryCatchPattern":"try {\n  writer.flush();\n} catch (IllegalStateException e) {\n  if (e.getMessage().equals(\"JsonWriter is closed.\")) {\n    // writer already closed; nothing to do, or recreate\n  } else throw e;\n}","preventionTips":["Call close() exactly once, as the final operation on the writer.","Do not close the underlying Writer/OutputStream separately from JsonWriter.close().","Avoid writing in finally blocks that may run after close().","Recreate the writer over a fresh destination if more output is needed."],"tags":["gson","jsonwriter","lifecycle","resource-management"],"analyzedSha":"8b8628c65699bc4421696183c62ae0c1b9b281dc","analyzedAt":"2026-08-04T19:12:22.202Z","schemaVersion":2}