{"id":"adba28d5a27297f0","repo":"google/gson","slug":"getclass-getsimplename","errorCode":null,"errorMessage":"{getClass().getSimpleName()}","messagePattern":"\\{getClass\\(\\)\\.getSimpleName\\(\\)\\}","errorType":"exception","errorClass":"UnsupportedOperationException","httpStatus":null,"severity":"error","filePath":"gson/src/main/java/com/google/gson/JsonElement.java","lineNumber":224,"sourceCode":"  @CanIgnoreReturnValue // When this method is used only to verify that the value is JsonNull\n  public JsonNull getAsJsonNull() {\n    if (isJsonNull()) {\n      return (JsonNull) this;\n    }\n    throw new IllegalStateException(\"Not a JSON Null: \" + this);\n  }\n\n  /**\n   * Convenience method to get this element as a boolean value.\n   *\n   * @return this element as a primitive boolean value.\n   * @throws UnsupportedOperationException if this element is not a {@link JsonPrimitive} or {@link\n   *     JsonArray}.\n   * @throws IllegalStateException if this element is of the type {@link JsonArray} but contains\n   *     more than a single element.\n   */\n  public boolean getAsBoolean() {\n    throw new UnsupportedOperationException(getClass().getSimpleName());\n  }\n\n  /**\n   * Convenience method to get this element as a {@link Number}.\n   *\n   * @return this element as a {@link Number}.\n   * @throws UnsupportedOperationException if this element is not a {@link JsonPrimitive} or {@link\n   *     JsonArray}, or cannot be converted to a number.\n   * @throws IllegalStateException if this element is of the type {@link JsonArray} but contains\n   *     more than a single element.\n   */\n  public Number getAsNumber() {\n    throw new UnsupportedOperationException(getClass().getSimpleName());\n  }\n\n  /**\n   * Convenience method to get this element as a string value.\n   *","sourceCodeStart":206,"sourceCodeEnd":242,"githubUrl":"https://github.com/google/gson/blob/8b8628c65699bc4421696183c62ae0c1b9b281dc/gson/src/main/java/com/google/gson/JsonElement.java#L206-L242","documentation":"Default JsonElement.getAsBoolean() throws UnsupportedOperationException(getClass().getSimpleName()) so the exception message is the actual subclass name (e.g. 'JsonObject', 'JsonNull'). It is overridden only by JsonPrimitive and JsonArray; calling it on a JsonObject or JsonNull fails.","triggerScenarios":"Calling jsonElement.getAsBoolean() (directly or via a generic loop) when the element is a JsonObject or JsonNull rather than a JsonPrimitive or single-element JsonArray.","commonSituations":"Iterating the values of a JsonObject and calling getAsBoolean() on each regardless of type; a field that is null where a boolean was expected; reading config that can be an object describing the flag.","solutions":["Guard with element.isJsonPrimitive() (and check the primitive's boolean-ness) before getAsBoolean().","Handle isJsonNull() with a default boolean.","Use element.getAsJsonPrimitive().getAsBoolean() only after a type check.","Deserialize to a Boolean/boolean POJO field for stable schemas."],"exampleFix":"// before\nfor (Map.Entry<String, JsonElement> e : obj.entrySet()) {\n  boolean b = e.getValue().getAsBoolean(); // throws on null/object\n}\n// after\nJsonElement v = e.getValue();\nboolean b = v.isJsonPrimitive() ? v.getAsBoolean() : false;","handlingStrategy":"type-guard","validationCode":"JsonElement v = el;\nif (!v.isJsonPrimitive()) {\n  throw new IllegalStateException(\"expected primitive boolean, got \" + v.getClass().getSimpleName());\n}\nboolean b = v.getAsBoolean();","typeGuard":"boolean asBoolOrDefault(JsonElement e, boolean def) {\n  return (e != null && e.isJsonPrimitive()) ? e.getAsBoolean() : def;\n}","tryCatchPattern":"try {\n  boolean b = el.getAsBoolean();\n} catch (UnsupportedOperationException ex) {\n  // message is subclass name; default / skip\n}","preventionTips":["Check isJsonPrimitive() before scalar accessors.","Handle JSON null explicitly for optional booleans.","Prefer typed deserialization for known schemas."],"tags":["gson","json-element","type-mismatch","boolean"],"analyzedSha":"8b8628c65699bc4421696183c62ae0c1b9b281dc","analyzedAt":"2026-08-04T19:12:22.202Z","schemaVersion":2}