{"record":{"id":"0e3c1b3363f7d4c9","repo":"google/gson","slug":"string-value-is-empty","errorCode":null,"errorMessage":"String value is empty","messagePattern":"String value is empty","errorType":"exception","errorClass":"UnsupportedOperationException","httpStatus":null,"severity":"warning","filePath":"gson/src/main/java/com/google/gson/JsonPrimitive.java","lineNumber":253,"sourceCode":"  /**\n   * @throws NumberFormatException {@inheritDoc}\n   */\n  @Override\n  public byte getAsByte() {\n    return isNumber() ? getAsNumber().byteValue() : Byte.parseByte(getAsString());\n  }\n\n  /**\n   * @throws UnsupportedOperationException if the string value of this primitive is empty.\n   * @deprecated This method is misleading, as it does not get this element as a char but rather as\n   *     a string's first character.\n   */\n  @Deprecated\n  @Override\n  public char getAsCharacter() {\n    String s = getAsString();\n    if (s.isEmpty()) {\n      throw new UnsupportedOperationException(\"String value is empty\");\n    } else {\n      return s.charAt(0);\n    }\n  }\n\n  /** Returns the hash code of this object. */\n  @Override\n  public int hashCode() {\n    if (value == null) {\n      return 31;\n    }\n    // The conditions below parallel the structure of equals(Object). Unlike in equals, every\n    // numeric branch must delegate to the same double-based hash: equals can consider primitives\n    // from *different* branches equal (for example Integer 42 equals Double 42.0 and a lazily\n    // parsed number 42 through the floating-point comparison), so a branch hashing anything other\n    // than the double value would give equal primitives different hash codes, see\n    // https://github.com/google/gson/issues/992\n    if (isIntegral(this)) {","sourceCodeStart":235,"sourceCodeEnd":271,"githubUrl":"https://github.com/google/gson/blob/310ac341f2f92a454b229bf21f70d2d18b2b6db7/gson/src/main/java/com/google/gson/JsonPrimitive.java#L235-L271","documentation":"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.","triggerScenarios":"new JsonPrimitive(\"\").getAsCharacter(); or a JSON string field that is empty being read as a char.","commonSituations":"Legacy code using the deprecated getAsCharacter(); empty/blank string fields where a single char was expected; config values left blank.","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"],"exampleFix":"// before\nchar c = primitive.getAsCharacter(); // throws if value is \"\"\n// after\nString s = primitive.getAsString();\nchar c = s.isEmpty() ? '\\0' : s.charAt(0);","handlingStrategy":"validation","validationCode":"JsonPrimitive p = ...;\nString s = p.getAsString();\nboolean hasChar = !s.isEmpty();","typeGuard":"static Character firstCharOrNull(JsonPrimitive p) {\n    String s = p.getAsString();\n    return s.isEmpty() ? null : s.charAt(0);\n}","tryCatchPattern":"try {\n    char c = primitive.getAsCharacter();\n} catch (UnsupportedOperationException ex) {\n    // \"String value is empty\"\n}","preventionTips":["Avoid the deprecated getAsCharacter(); use getAsString() with a length check","Validate single-char fields at the source","Default safely when the string is empty"],"tags":["gson","json","deprecated","unsupported-operation"],"backgroundTag":null,"analyzedSha":"310ac341f2f92a454b229bf21f70d2d18b2b6db7","analyzedAt":"2026-08-10T02:58:47.455Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}