{"record":{"id":"105b627ae7251f05","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/310ac341f2f92a454b229bf21f70d2d18b2b6db7/gson/src/main/java/com/google/gson/JsonElement.java#L206-L242","documentation":"Base implementation of JsonElement.getAsBoolean() (JsonElement.java:224) throws UnsupportedOperationException(getClass().getSimpleName()); the message is just the runtime class name (e.g. 'JsonObject' or 'JsonNull'). JsonPrimitive and JsonArray override this method, so the throw only fires when called on a JsonObject or JsonNull. The method is meant to extract a boolean from a scalar.","triggerScenarios":"Calling getAsBoolean() on a JsonObject or on a JsonNull. Common: root.get(\"flag\").getAsBoolean() where 'flag' is explicitly JSON null, or where the wrong element (a container) was retrieved.","commonSituations":"A nullable boolean field is null in the payload and code chains .getAsBoolean() without a check; wrong tree node retrieved; schema mismatch where a boolean path is actually an object.","solutions":["Guard with element.isJsonPrimitive() (and null-check) before calling getAsBoolean()","Handle JsonNull explicitly (e.g. map to a default or Optional.empty)","Confirm the field's actual type in the payload","Use a typed POJO with a Boolean (nullable) field via gson.fromJson"],"exampleFix":"// before\nboolean active = root.get(\"active\").getAsBoolean();\n// after\nJsonElement e = root.get(\"active\");\nboolean active = (e != null && e.isJsonPrimitive()) ? e.getAsBoolean() : false;","handlingStrategy":"type-guard","validationCode":"JsonElement e = root.get(\"active\");\nboolean safe = (e != null && e.isJsonPrimitive());","typeGuard":"static boolean asBool(JsonElement e, boolean dflt) {\n    return (e != null && e.isJsonPrimitive()) ? e.getAsBoolean() : dflt;\n}","tryCatchPattern":"try {\n    boolean b = element.getAsBoolean();\n} catch (UnsupportedOperationException ex) {\n    // ex.getMessage() is the class name, e.g. \"JsonNull\" or \"JsonObject\"\n}","preventionTips":["Null-check and call isJsonPrimitive() before getAsBoolean()","Handle JsonNull with a sensible default","Map nullable booleans to a typed POJO Boolean field"],"tags":["gson","json","type-cast","unsupported-operation"],"backgroundTag":null,"analyzedSha":"310ac341f2f92a454b229bf21f70d2d18b2b6db7","analyzedAt":"2026-08-10T02:58:47.455Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}