{"record":{"id":"ef6dd6ab02333591","repo":"google/gson","slug":"not-a-json-null-this","errorCode":null,"errorMessage":"Not a JSON Null: ${this}","messagePattern":"Not a JSON Null: (.+?)","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"warning","filePath":"gson/src/main/java/com/google/gson/JsonElement.java","lineNumber":211,"sourceCode":"    }\n    throw new IllegalStateException(\"Not a JSON Primitive: \" + this);\n  }\n\n  /**\n   * Convenience method to get this element as a {@link JsonNull}. 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 #isJsonNull()} first.\n   *\n   * @return this element as a {@link JsonNull}.\n   * @throws IllegalStateException if this element is of another type.\n   * @since 1.2\n   */\n  @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   *","sourceCodeStart":193,"sourceCodeEnd":229,"githubUrl":"https://github.com/google/gson/blob/310ac341f2f92a454b229bf21f70d2d18b2b6db7/gson/src/main/java/com/google/gson/JsonElement.java#L193-L229","documentation":"Thrown by JsonElement.getAsJsonNull() (JsonElement.java:211) as an IllegalStateException when the element is not a JsonNull. Returns (JsonNull) this only when isJsonNull() is true. This method is typically used to assert/verify that a value is the JSON null literal.","triggerScenarios":"Calling getAsJsonNull() on any non-null element — a JsonObject, JsonArray, or JsonPrimitive. Often an assertion-style call meant to confirm a field is null when it actually carries a value.","commonSituations":"Validation logic that expects a field to be absent/null but the payload populates it; checking a deletion/absence marker that now has a value.","solutions":["Guard with element.isJsonNull() before calling getAsJsonNull()","In most cases just call isJsonNull() directly and skip getAsJsonNull()","Re-check the schema: the field was expected to be null but is populated"],"exampleFix":"// before\nJsonNull n = root.get(\"deleted_at\").getAsJsonNull();\n// after\nboolean isNull = root.has(\"deleted_at\") && root.get(\"deleted_at\").isJsonNull();","handlingStrategy":"type-guard","validationCode":"JsonElement e = root.get(\"deleted_at\");\nboolean isNull = (e != null && e.isJsonNull());","typeGuard":"static boolean valueIsNull(JsonElement e) {\n    return e != null && e.isJsonNull();\n}","tryCatchPattern":"try {\n    JsonNull n = element.getAsJsonNull();\n} catch (IllegalStateException ex) {\n    // message starts with \"Not a JSON Null:\" -> element actually has a value\n}","preventionTips":["Use isJsonNull() directly instead of getAsJsonNull() for checks","Distinguish absent (get returns null) from explicit null (get returns JsonNull)","Validate the schema when a field is expected to be null but carries a value"],"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"}