{"record":{"id":"ebc51296096dd51c","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/310ac341f2f92a454b229bf21f70d2d18b2b6db7/gson/src/main/java/com/google/gson/JsonElement.java#L176-L212","documentation":"Thrown by JsonElement.getAsJsonPrimitive() (JsonElement.java:194) as an IllegalStateException when the element is not a JsonPrimitive (i.e. it is a JsonObject, JsonArray, or JsonNull). Returns (JsonPrimitive) this only when isJsonPrimitive() is true.","triggerScenarios":"Calling getAsJsonPrimitive() on a container element or on JsonNull. E.g. obj.get(\"version\").getAsJsonPrimitive() when 'version' is itself an object, an array, or explicitly null.","commonSituations":"Code that retrieves a leaf scalar but the field is unexpectedly structured (object/array); a field that is null; type assumptions about loosely-typed JSON.","solutions":["Guard with element.isJsonPrimitive() (and null-check) before calling getAsJsonPrimitive()","Usually you can call the specific accessor (getAsString()/getAsInt()) directly once isJsonPrimitive() holds","Handle JsonNull explicitly","Prefer a typed POJO via gson.fromJson"],"exampleFix":"// before\nJsonPrimitive p = root.get(\"version\").getAsJsonPrimitive();\n// after\nJsonElement e = root.get(\"version\");\nif (e != null && e.isJsonPrimitive()) {\n    JsonPrimitive p = e.getAsJsonPrimitive();\n}","handlingStrategy":"type-guard","validationCode":"JsonElement e = root.get(\"version\");\nboolean safe = (e != null && e.isJsonPrimitive());","typeGuard":"static JsonPrimitive asPrimitive(JsonElement e) {\n    return (e != null && e.isJsonPrimitive()) ? e.getAsJsonPrimitive() : null;\n}","tryCatchPattern":"try {\n    JsonPrimitive p = element.getAsJsonPrimitive();\n} catch (IllegalStateException ex) {\n    // message starts with \"Not a JSON Primitive:\"\n}","preventionTips":["Call isJsonPrimitive() before getAsJsonPrimitive()","Usually prefer the specific scalar accessor once you have confirmed a primitive","Handle JsonNull explicitly"],"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"}