{"id":"0138dbbe6cd53cc6","repo":"google/gson","slug":"not-a-json-primitive-this","errorCode":null,"errorMessage":"Not a JSON Primitive: {this}","messagePattern":"Not a JSON Primitive: (.+?)","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"gson/src/main/java/com/google/gson/JsonElement.java","lineNumber":194,"sourceCode":"      return (JsonArray) this;\n    }\n    throw new IllegalStateException(\"Not a JSON Array: \" + this);\n  }\n\n  /**\n   * Convenience method to get this element as a {@link JsonPrimitive}. 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 #isJsonPrimitive()}\n   * first.\n   *\n   * @return this element as a {@link JsonPrimitive}.\n   * @throws IllegalStateException if this element is of another type.\n   */\n  public JsonPrimitive getAsJsonPrimitive() {\n    if (isJsonPrimitive()) {\n      return (JsonPrimitive) this;\n    }\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  }","sourceCodeStart":176,"sourceCodeEnd":212,"githubUrl":"https://github.com/google/gson/blob/8b8628c65699bc4421696183c62ae0c1b9b281dc/gson/src/main/java/com/google/gson/JsonElement.java#L176-L212","documentation":"Thrown by JsonElement.getAsJsonPrimitive() when the element is not a JsonPrimitive (e.g. it is an object, array, or null). Used when the caller wants the primitive holder to then extract a typed scalar.","triggerScenarios":"Calling element.getAsJsonPrimitive() on a JsonObject, JsonArray, or JsonNull.","commonSituations":"Iterating map values and assuming all are scalars; a field that is sometimes null; a nested object where a primitive was expected.","solutions":["Guard with element.isJsonPrimitive() first.","Handle isJsonNull() for nullable scalar fields.","Branch on isJsonObject()/isJsonArray() if the field is polymorphic."],"exampleFix":"// before\nJsonPrimitive p = el.getAsJsonPrimitive();\n// after\nif (el.isJsonPrimitive()) {\n  JsonPrimitive p = el.getAsJsonPrimitive();\n} else if (el.isJsonNull()) {\n  // null scalar\n}","handlingStrategy":"type-guard","validationCode":"if (!element.isJsonPrimitive()) {\n  throw new IllegalStateException(\"expected JSON primitive, got \" + element.getClass().getSimpleName());\n}\nJsonPrimitive p = element.getAsJsonPrimitive();","typeGuard":"boolean isPrimitiveValue(JsonElement e) { return e != null && e.isJsonPrimitive(); }","tryCatchPattern":"try {\n  JsonPrimitive p = el.getAsJsonPrimitive();\n} catch (IllegalStateException e) {\n  // default / skip non-primitive value\n}","preventionTips":["isJsonPrimitive() before getAsJsonPrimitive().","Handle JSON null explicitly for nullable scalars.","Prefer typed POJO deserialization for stable fields."],"tags":["gson","json-element","type-mismatch","parsing"],"analyzedSha":"8b8628c65699bc4421696183c62ae0c1b9b281dc","analyzedAt":"2026-08-04T19:12:22.202Z","schemaVersion":2}