{"record":{"id":"29a13951473b68dc","repo":"apple/pkl","slug":"cannot-convert-pkl-base-string-s-to-java-lang-c","errorCode":null,"errorMessage":"Cannot convert pkl.base#String `%s` to java.lang.Character because it is not of length 1.","messagePattern":"Cannot convert pkl\\.base#String `(.+?)` to java\\.lang\\.Character because it is not of length 1\\.","errorType":"exception","errorClass":"ConversionException","httpStatus":null,"severity":"error","filePath":"pkl-config-java/src/main/java/org/pkl/config/java/mapper/Conversions.java","lineNumber":124,"sourceCode":"  public static final Conversion<Double, Float> pFloatToFloat =\n      Conversion.of(PClassInfo.Float, float.class, (value, mapper) -> value.floatValue());\n\n  /** Conversion from {@code pkl.base#Float} to {@link BigDecimal}. */\n  public static final Conversion<Double, BigDecimal> pFloatToBigDecimal =\n      Conversion.of(\n          PClassInfo.Float, BigDecimal.class, (value, mapper) -> BigDecimal.valueOf(value));\n\n  /**\n   * Conversion from {@code pkl.base#String} to {@link Character}. Throws {@link\n   * ConversionException} if the String value is not of length one.\n   */\n  public static final Conversion<String, Character> pStringToCharacter =\n      Conversion.of(\n          PClassInfo.String,\n          Character.class,\n          (value, mapper) -> {\n            if (value.length() != 1) {\n              throw new ConversionException(\n                  String.format(\n                      \"Cannot convert pkl.base#String `%s` to java.lang.Character because it is not of length 1.\",\n                      value));\n            }\n            return value.charAt(0);\n          });\n\n  /**\n   * Conversion from {@code pkl.base#String} to {@link URI}. Throws {@link ConversionException} if\n   * the String value is not a syntactically valid URI.\n   */\n  public static final Conversion<String, URI> pStringToURI =\n      Conversion.of(\n          PClassInfo.String,\n          URI.class,\n          (value, mapper) -> {\n            try {\n              return new URI(value);","sourceCodeStart":106,"sourceCodeEnd":142,"githubUrl":"https://github.com/apple/pkl/blob/f3efcbfc9b60d30053b0536d664948d7aa1b8673/pkl-config-java/src/main/java/org/pkl/config/java/mapper/Conversions.java#L106-L142","documentation":"Conversions.pStringToCharacter maps a Pkl String to a Java Character and throws ConversionException unless the string is exactly one character long. Since there is no Pkl char type, single-character strings are the convention, and this check prevents silently taking charAt(0) of a longer string.","triggerScenarios":"Mapping a Pkl String property to a Java char/Character field where the .pkl value has length 0 or >= 2, e.g. a separator, mode flag, or currency symbol accidentally written as a multi-character or empty string.","commonSituations":"Writing \"\" instead of a real character in the .pkl config; a value like \"--\" or \"ab\" where a single delimiter was intended; locale/keyboard mistakes; generated Java schema declaring Character for what the Pkl data treats as free text.","solutions":["Fix the .pkl value to be exactly one character, or correct the intended single-char constant.","Change the Java target type to String if multi-character values are legitimate, and update the mapping.","Validate the string length in Pkl itself: sep: String(length == 1) (or String(length(this) == 1) as applicable) so bad values fail at evaluation.","Trim/normalize the value before mapping if whitespace may be inflating the length."],"exampleFix":"// before: in .pkl\nseparator: String = \"::\" // length 2 -> ConversionException\n// after\nseparator: String(length == 1) = \":\"","handlingStrategy":"validation","validationCode":"// validate before mapping to Character\nstatic char checkedToChar(String value) {\n  if (value == null || value.length() != 1)\n    throw new IllegalArgumentException(\"expected single character, got: '\" + value + \"'\");\n  return value.charAt(0);\n}","typeGuard":"static boolean isSingleChar(String s) { return s != null && s.length() == 1; }","tryCatchPattern":"try {\n  Character c = mapper.map(value, Character.class);\n} catch (ConversionException e) {\n  log.error(\"expected 1-char Pkl String: {}\", e.getMessage());\n  throw new ConfigValidationException(\"separator/mode must be a single character\", e);\n}","preventionTips":["Constrain in Pkl: prop: String(length == 1)","Use String in the Java schema when values may be longer","Trim values in .pkl to avoid accidental whitespace making length 2","Cover single-char config values with tests using both empty and multi-char inputs"],"tags":["conversion","string","character","pkl","mapping"],"backgroundTag":"invalid-argument-value","analyzedSha":"f3efcbfc9b60d30053b0536d664948d7aa1b8673","analyzedAt":"2026-09-08T13:10:45.570Z","contentChangedAt":"2026-09-08T13:10:45.570Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}