{"record":{"id":"b1a3a9d1807b78f7","repo":"apple/pkl","slug":"cannotparsestringas","errorCode":"cannotParseStringAs","errorMessage":"cannotParseStringAs","messagePattern":"cannotParseStringAs","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkl-core/src/main/java/org/pkl/core/stdlib/base/StringNodes.java","lineNumber":888,"sourceCode":"      // ensure no trailing garbage\n      if (lexer.next() != Token.EOF) {\n        throw new NumberFormatException();\n      }\n\n      return parsed;\n    } catch (ParserError ignored) {\n      throw new NumberFormatException();\n    }\n  }\n\n  public abstract static class toInt extends ExternalMethod0Node {\n    @TruffleBoundary\n    @Specialization\n    protected long eval(String self) {\n      try {\n        return toInt(self);\n      } catch (NumberFormatException e) {\n        throw exceptionBuilder()\n            .evalError(\"cannotParseStringAs\", \"Int\")\n            .withProgramValue(\"String\", self)\n            .build();\n      }\n    }\n  }\n\n  public abstract static class toIntOrNull extends ExternalMethod0Node {\n    @TruffleBoundary\n    @Specialization\n    protected Object eval(String self) {\n      try {\n        return toInt(self);\n      } catch (NumberFormatException e) {\n        return VmNull.withoutDefault();\n      }\n    }\n  }","sourceCodeStart":870,"sourceCodeEnd":906,"githubUrl":"https://github.com/apple/pkl/blob/f3efcbfc9b60d30053b0536d664948d7aa1b8673/pkl-core/src/main/java/org/pkl/core/stdlib/base/StringNodes.java#L870-L906","documentation":"Thrown by String.toInt() when the string cannot be parsed as an integer. Pkl uses Java's integer parsing (after removing underscores), so any non-integer text — including floats, hex without prefix support, or surrounding whitespace — raises this error with the offending string attached.","triggerScenarios":"Calling `\"abc\".toInt()`, `\"3.14\".toInt()`, `\" 42 \".toInt()` (whitespace not trimmed), or an empty string; also integers out of the 64-bit Long range.","commonSituations":"Reading values from config files, environment variables, or user input assumed numeric; decimals coming from JSON/YAML strings; localized numbers with separators.","solutions":["Trim and validate the string contains only an optional sign and digits before calling toInt","Use toFloat() first if the value may be decimal, then convert","Provide a default: `s.toIntOrNull() ?? default` if available in your Pkl version","Reject or sanitize commas/separators before parsing"],"exampleFix":"// before\nval n = userInput.toInt()\n// after\nval n = userInput.trim().replace(\",\", \"\").toIntOrNull() ?? 0","handlingStrategy":"validation","validationCode":"function isIntString(s: String): Boolean = s.trim().isRegexMatch(/^[+-]?\\d+$/, _)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Trim and validate numeric strings before parsing","Use toIntOrNull with a fallback default","Watch for thousand separators and whitespace from external data"],"tags":["string","parsing","int"],"backgroundTag":"cannot-parse-string-as","analyzedSha":"f3efcbfc9b60d30053b0536d664948d7aa1b8673","analyzedAt":"2026-09-08T13:10:45.570Z","contentChangedAt":"2026-09-08T13:10:45.570Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}