{"id":"7d1dc2c8c7062769","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":"error","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/8b8628c65699bc4421696183c62ae0c1b9b281dc/gson/src/main/java/com/google/gson/JsonElement.java#L193-L229","documentation":"Thrown by JsonElement.getAsJsonNull() when the element is not a JsonNull. This accessor is mostly used to assert/verify that a value is the JSON null token; calling it on any other element type fails.","triggerScenarios":"Calling element.getAsJsonNull() on a non-null element to 'confirm' nullness, or misusing it instead of isJsonNull().","commonSituations":"Confusing getAsJsonNull() (a cast) with isJsonNull() (a predicate); defensive code that calls getAsJsonNull() without first checking.","solutions":["Use isJsonNull() as the predicate instead of calling getAsJsonNull() to test for null.","Only call getAsJsonNull() after isJsonNull() returns true, if you need the typed JsonNull reference.","For nullable fields, branch on isJsonNull() and skip processing."],"exampleFix":"// before\nif (el.getAsJsonNull() != null) { ... } // throws if el is not null\n// after\nif (el.isJsonNull()) { ... }","handlingStrategy":"validation","validationCode":"// Use the predicate, not the cast, to test for null\nboolean isNull = el != null && el.isJsonNull();","typeGuard":"boolean isJsonValueNull(JsonElement e) { return e != null && e.isJsonNull(); }","tryCatchPattern":null,"preventionTips":["Prefer isJsonNull() over getAsJsonNull() for nullness checks.","Treat getAsJsonNull() as a cast, not a predicate."],"tags":["gson","json-element","type-mismatch","null-handling"],"analyzedSha":"8b8628c65699bc4421696183c62ae0c1b9b281dc","analyzedAt":"2026-08-04T19:12:22.202Z","schemaVersion":2}