{"record":{"id":"8fefa9bcead49be0","repo":"google/gson","slug":"not-a-json-object-this","errorCode":null,"errorMessage":"Not a JSON Object: ${this}","messagePattern":"Not a JSON Object: (.+?)","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"gson/src/main/java/com/google/gson/JsonElement.java","lineNumber":163,"sourceCode":"   */\n  public boolean isJsonNull() {\n    return this instanceof JsonNull;\n  }\n\n  /**\n   * Convenience method to get this element as a {@link JsonObject}. If this element is of some\n   * other type, an {@link IllegalStateException} will result. Hence it is best to use this method\n   * after ensuring that this element is of the desired type by calling {@link #isJsonObject()}\n   * first.\n   *\n   * @return this element as a {@link JsonObject}.\n   * @throws IllegalStateException if this element is of another type.\n   */\n  public JsonObject getAsJsonObject() {\n    if (isJsonObject()) {\n      return (JsonObject) this;\n    }\n    throw new IllegalStateException(\"Not a JSON Object: \" + this);\n  }\n\n  /**\n   * Convenience method to get this element as a {@link JsonArray}. If this element is of some other\n   * type, an {@link IllegalStateException} will result. Hence it is best to use this method after\n   * ensuring that this element is of the desired type by calling {@link #isJsonArray()} first.\n   *\n   * @return this element as a {@link JsonArray}.\n   * @throws IllegalStateException if this element is of another type.\n   */\n  public JsonArray getAsJsonArray() {\n    if (isJsonArray()) {\n      return (JsonArray) this;\n    }\n    throw new IllegalStateException(\"Not a JSON Array: \" + this);\n  }\n\n  /**","sourceCodeStart":145,"sourceCodeEnd":181,"githubUrl":"https://github.com/google/gson/blob/310ac341f2f92a454b229bf21f70d2d18b2b6db7/gson/src/main/java/com/google/gson/JsonElement.java#L145-L181","documentation":"Thrown by JsonElement.getAsJsonObject() (JsonElement.java:163) as an IllegalStateException when the element is not a JsonObject. The method is a convenience cast: it returns (JsonObject) this when isJsonObject() is true (i.e. this instanceof JsonObject), otherwise throws with the element's toString appended. The four JsonElement subtypes are JsonObject, JsonArray, JsonPrimitive and JsonNull; only JsonObject passes.","triggerScenarios":"Calling getAsJsonObject() on a JsonElement that parsed to an array, primitive, or null. Most often obj.get(\"key\").getAsJsonObject() where 'key' is explicitly JSON null (JsonNull) or where the API returned an array/string where an object was expected. Also JsonParser.parseString(\"[]\").getAsJsonObject().","commonSituations":"A REST API changed a field from object to array or scalar; a field is conditionally null in the payload; loosely-typed third-party JSON; treating a one-object list as an object. Distinct from a Java NullPointerException (absent key returns null) — this fires on a present-but-wrong-type or JsonNull element.","solutions":["Guard with element.isJsonObject() (and a Java null-check) before calling getAsJsonObject()","Handle the explicitly-null case with element.isJsonNull() / null separately","Inspect the raw JSON payload to confirm the field's actual runtime type","Deserialize directly into a typed POJO with gson.fromJson(json, MyClass.class) instead of manual tree navigation"],"exampleFix":"// before\nJsonObject data = root.get(\"data\").getAsJsonObject();\n// after\nJsonElement data = root.get(\"data\");\nif (data != null && data.isJsonObject()) {\n    JsonObject obj = data.getAsJsonObject();\n} else {\n    // handle missing / wrong type / explicit null\n}","handlingStrategy":"type-guard","validationCode":"JsonElement e = root.get(\"data\");\nboolean safe = (e != null && e.isJsonObject());\n// only call getAsJsonObject() when `safe` is true","typeGuard":"static JsonObject asObject(JsonElement e) {\n    return (e != null && e.isJsonObject()) ? e.getAsJsonObject() : null;\n}","tryCatchPattern":"try {\n    JsonObject o = element.getAsJsonObject();\n} catch (IllegalStateException ex) {\n    // message starts with \"Not a JSON Object:\"\n    // fall back: treat as missing/wrong-type\n}","preventionTips":["Always null-check and call isJsonObject() before getAsJsonObject()","Prefer gson.fromJson(payload, MyClass.class) with a typed model over manual tree walks","Unit-test parsing against the real API response shape including null fields"],"tags":["gson","json","type-cast","illegal-state"],"backgroundTag":null,"analyzedSha":"310ac341f2f92a454b229bf21f70d2d18b2b6db7","analyzedAt":"2026-08-10T02:58:47.455Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}