google/gson · error · IllegalStateException
Not a JSON Primitive: {this}
Error message
Not a JSON Primitive: {this} What it means
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.
Source
Thrown at gson/src/main/java/com/google/gson/JsonElement.java:194
return (JsonArray) this;
}
throw new IllegalStateException("Not a JSON Array: " + this);
}
/**
* Convenience method to get this element as a {@link JsonPrimitive}. If this element is of some
* other type, an {@link IllegalStateException} will result. Hence it is best to use this method
* after ensuring that this element is of the desired type by calling {@link #isJsonPrimitive()}
* first.
*
* @return this element as a {@link JsonPrimitive}.
* @throws IllegalStateException if this element is of another type.
*/
public JsonPrimitive getAsJsonPrimitive() {
if (isJsonPrimitive()) {
return (JsonPrimitive) this;
}
throw new IllegalStateException("Not a JSON Primitive: " + this);
}
/**
* Convenience method to get this element as a {@link JsonNull}. If this element is of some other
* type, an {@link IllegalStateException} will result. Hence it is best to use this method after
* ensuring that this element is of the desired type by calling {@link #isJsonNull()} first.
*
* @return this element as a {@link JsonNull}.
* @throws IllegalStateException if this element is of another type.
* @since 1.2
*/
@CanIgnoreReturnValue // When this method is used only to verify that the value is JsonNull
public JsonNull getAsJsonNull() {
if (isJsonNull()) {
return (JsonNull) this;
}
throw new IllegalStateException("Not a JSON Null: " + this);
}View on GitHub (pinned to 8b8628c656)
Solutions
- Guard with element.isJsonPrimitive() first.
- Handle isJsonNull() for nullable scalar fields.
- Branch on isJsonObject()/isJsonArray() if the field is polymorphic.
Example fix
// before
JsonPrimitive p = el.getAsJsonPrimitive();
// after
if (el.isJsonPrimitive()) {
JsonPrimitive p = el.getAsJsonPrimitive();
} else if (el.isJsonNull()) {
// null scalar
} Defensive patterns
Strategy: type-guard
Validate before calling
if (!element.isJsonPrimitive()) {
throw new IllegalStateException("expected JSON primitive, got " + element.getClass().getSimpleName());
}
JsonPrimitive p = element.getAsJsonPrimitive(); Type guard
boolean isPrimitiveValue(JsonElement e) { return e != null && e.isJsonPrimitive(); } Try / catch
try {
JsonPrimitive p = el.getAsJsonPrimitive();
} catch (IllegalStateException e) {
// default / skip non-primitive value
} Prevention
- isJsonPrimitive() before getAsJsonPrimitive().
- Handle JSON null explicitly for nullable scalars.
- Prefer typed POJO deserialization for stable fields.
When it happens
Trigger: Calling element.getAsJsonPrimitive() on a JsonObject, JsonArray, or JsonNull.
Common situations: Iterating map values and assuming all are scalars; a field that is sometimes null; a nested object where a primitive was expected.
Related errors
- Not a JSON Object: {this}
- Not a JSON Array: {this}
- Array must have size 1, but has size {size}
- Not a JSON Null: {this}
- {getClass().getSimpleName()}
AI-assisted analysis of google/gson@8b8628c656 (2026-08-04).
Data as JSON: /data/errors/0138dbbe6cd53cc6.json.
Report an issue: GitHub.