google/gson · warning · UnsupportedOperationException
String value is empty
Error message
String value is empty
What it means
Thrown by JsonPrimitive.getAsCharacter() (JsonPrimitive.java:253) as UnsupportedOperationException when the primitive's string value is empty — charAt(0) has nothing to return. The method is @Deprecated because it returns the first character of the string representation, not a true char conversion.
Solutions
- Stop using the deprecated getAsCharacter(); read getAsString() and check length before charAt(0)
- Guard with a non-empty check on the string value
- Validate the source emits a single-character string for the field
Example fix
// before char c = primitive.getAsCharacter(); // throws if value is "" // after String s = primitive.getAsString(); char c = s.isEmpty() ? '\0' : s.charAt(0);
Defensive patterns
Strategy: validation
Validate before calling
JsonPrimitive p = ...; String s = p.getAsString(); boolean hasChar = !s.isEmpty();
Type guard
static Character firstCharOrNull(JsonPrimitive p) {
String s = p.getAsString();
return s.isEmpty() ? null : s.charAt(0);
} Try / catch
try {
char c = primitive.getAsCharacter();
} catch (UnsupportedOperationException ex) {
// "String value is empty"
} Prevention
- Avoid the deprecated getAsCharacter(); use getAsString() with a length check
- Validate single-char fields at the source
- Default safely when the string is empty
When it happens
Trigger: new JsonPrimitive("").getAsCharacter(); or a JSON string field that is empty being read as a char.
Common situations: Legacy code using the deprecated getAsCharacter(); empty/blank string fields where a single char was expected; config values left blank.
Related errors
- ${getClass().getSimpleName()}
- Primitive is neither a number nor a string
- Did not consume the entire document.
- Failed parsing JSON source
- Incomplete document
AI-assisted analysis of google/gson@310ac341f2 (2026-08-10).
Data as JSON: /api/errors/0e3c1b3363f7d4c9.
Report an issue: GitHub.
Appendix: source
Thrown at gson/src/main/java/com/google/gson/JsonPrimitive.java:253
/**
* @throws NumberFormatException {@inheritDoc}
*/
@Override
public byte getAsByte() {
return isNumber() ? getAsNumber().byteValue() : Byte.parseByte(getAsString());
}
/**
* @throws UnsupportedOperationException if the string value of this primitive is empty.
* @deprecated This method is misleading, as it does not get this element as a char but rather as
* a string's first character.
*/
@Deprecated
@Override
public char getAsCharacter() {
String s = getAsString();
if (s.isEmpty()) {
throw new UnsupportedOperationException("String value is empty");
} else {
return s.charAt(0);
}
}
/** Returns the hash code of this object. */
@Override
public int hashCode() {
if (value == null) {
return 31;
}
// The conditions below parallel the structure of equals(Object). Unlike in equals, every
// numeric branch must delegate to the same double-based hash: equals can consider primitives
// from *different* branches equal (for example Integer 42 equals Double 42.0 and a lazily
// parsed number 42 through the floating-point comparison), so a branch hashing anything other
// than the double value would give equal primitives different hash codes, see
// https://github.com/google/gson/issues/992
if (isIntegral(this)) {View on GitHub (pinned to 310ac341f2)