{"record":{"id":"bf505940d2dba654","repo":"apple/pkl","slug":"formatexception-key-integer-ret-getclass","errorCode":null,"errorMessage":"FormatException(key, \"integer\", ret.getClass())","messagePattern":"FormatException\\(key, \"integer\", ret\\.getClass\\(\\)\\)","errorType":"exception","errorClass":"FormatException","httpStatus":null,"severity":"error","filePath":"pkl-core/src/main/java/org/pkl/core/util/json/Json.java","lineNumber":268,"sourceCode":"\n    public boolean getBoolean(String key) throws JsonParseException {\n      var ret = get(key);\n      if (ret == null) {\n        throw new MissingFieldException(this, key);\n      }\n      if (!(ret instanceof Boolean b)) {\n        throw new FormatException(key, \"boolean\", ret.getClass());\n      }\n      return b;\n    }\n\n    public int getInt(String key) throws JsonParseException {\n      var ret = get(key);\n      if (ret == null) {\n        throw new MissingFieldException(this, key);\n      }\n      if (!(ret instanceof Integer i)) {\n        throw new FormatException(key, \"integer\", ret.getClass());\n      }\n      return i;\n    }\n\n    public String getString(String key) throws JsonParseException {\n      var ret = getStringOrNull(key);\n      if (ret == null) {\n        throw new MissingFieldException(this, key);\n      }\n      return ret;\n    }\n\n    public @Nullable String getStringOrNull(String key) throws JsonParseException {\n      var ret = get(key);\n      if (ret == null) {\n        return null;\n      }\n      if (!(ret instanceof String string)) {","sourceCodeStart":250,"sourceCodeEnd":286,"githubUrl":"https://github.com/apple/pkl/blob/f3efcbfc9b60d30053b0536d664948d7aa1b8673/pkl-core/src/main/java/org/pkl/core/util/json/Json.java#L250-L286","documentation":"Thrown by JsObject.getInt(String) when the JSON field with the given key exists but holds a value that is not an Integer (e.g. a String, Double, Boolean, or nested object). The pkl JSON parser maps whole numbers to Integer, so any other Java type under that key fails the instanceof check. The FormatException names the key, the expected type 'integer', and the actual class to pinpoint the mismatch.","triggerScenarios":"Calling getInt(key) on a JsObject whose value for key parsed as a JSON string, floating-point number, boolean, array, or object instead of a whole-number JSON literal (e.g. {\"major\":\"3\"} or {\"count\":3.5}).","commonSituations":"A JSON document produced by another tool encodes numeric fields as strings; a schema change upstream switched a version/limit field from an integer to a decimal or string; hand-edited config quotes the number.","solutions":["Print the actual value type for the key (get(key).getClass()) and confirm what the JSON really contains","Read the field with getString + parse, or a numeric helper matching the real type (e.g. parse the string via Integer.parseInt)","Fix the producing side so the field is emitted as an unquoted whole number","Add a validation step before calling getInt that checks ret instanceof Integer"],"exampleFix":"// before: {\"major\": \"3\"}  -> obj.getInt(\"major\") throws\n// after: {\"major\": 3}\n// or in code:\n// before\nint major = obj.getInt(\"major\");\n// after\nint major = obj.getString(\"major\") != null ? Integer.parseInt(obj.getString(\"major\")) : obj.getInt(\"major\");","handlingStrategy":"type-guard","validationCode":"Object v = obj.get(\"major\");\nif (!(v instanceof Integer)) throw new IllegalStateException(\"field 'major' must be a whole number, got \" + (v == null ? \"null\" : v.getClass().getSimpleName()));","typeGuard":"boolean isIntField(JsObject o, String k) { return o.get(k) instanceof Integer; }","tryCatchPattern":"try { int n = obj.getInt(\"major\"); } catch (JsonParseException e) { /* log key/type mismatch, use default */ }","preventionTips":["Always confirm the JSON literal is an unquoted whole number before using getInt","Use get(key) + instanceof to inspect the parsed type when the schema is uncertain","Pin/validate the schema of incoming JSON documents before parsing fields"],"tags":["json","type-mismatch","pkl"],"backgroundTag":"type-mismatch","analyzedSha":"f3efcbfc9b60d30053b0536d664948d7aa1b8673","analyzedAt":"2026-09-08T13:10:45.570Z","contentChangedAt":"2026-09-08T13:10:45.570Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}