{"record":{"id":"26dc597748220cc2","repo":"Tencent/APIJSON","slug":"cannot-convert-value-of-type-value-getclass","errorCode":null,"errorMessage":"Cannot convert value of type \" + value.getClass().getName() + \" to int","messagePattern":"Cannot convert value of type \" \\+ value\\.getClass\\(\\)\\.getName\\(\\) \\+ \" to int","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"APIJSONORM/src/main/java/apijson/JSON.java","lineNumber":389,"sourceCode":"\tpublic static Integer getInteger(Map<String, Object> map, String key) throws IllegalArgumentException {\n\t\tObject value = map == null || key == null ? null : map.get(key);\n\t\tif (value == null) {\n\t\t\treturn null;\n\t\t}\n\n\t\tif (value instanceof Number) {\n\t\t\treturn ((Number) value).intValue();\n\t\t}\n\n\t\tif (value instanceof String) {\n\t\t\ttry {\n\t\t\t\treturn Integer.parseInt((String) value);\n\t\t\t} catch (NumberFormatException e) {\n\t\t\t\tthrow new IllegalArgumentException(\"Cannot convert String value '\" + value + \"' to int: \" + e.getMessage());\n\t\t\t}\n\t\t}\n\n\t\tthrow new IllegalArgumentException(\"Cannot convert value of type \" + value.getClass().getName() + \" to int\");\n\t}\n\n\t/**\n\t * Get an int value from a Map\n\t * @param map Source map\n\t * @param key The key\n\t * @return The int value\n\t * @throws IllegalArgumentException If value cannot be converted to int\n\t */\n\tpublic static int getIntValue(Map<String, Object> map, String key) throws IllegalArgumentException {\n\t\tObject value = map == null || key == null ? null : map.get(key);\n\t\tif (value == null) {\n\t\t\treturn 0;\n\t\t}\n\n\t\tif (value instanceof Number) {\n\t\t\treturn ((Number) value).intValue();\n\t\t}","sourceCodeStart":371,"sourceCodeEnd":407,"githubUrl":"https://github.com/Tencent/APIJSON/blob/5284052872898eddc449a58f629e5c8d588b8e22/APIJSONORM/src/main/java/apijson/JSON.java#L371-L407","documentation":"Thrown by apijson.JSON.getInteger(Map, String) (APIJSONORM/src/main/java/apijson/JSON.java:389) when the value at the key is neither null, nor a Number, nor a String — the two accepted source types. Anything else (Boolean, Map, List, byte[]) is rejected with the value's runtime class name in the message.","triggerScenarios":"The field holds a JSON object or array (e.g. {\"$\": 1} wrapper or [1,2]) where a scalar was expected; the value is a Boolean; the map was populated from a non-JSON source that stored an Enum, Date, or byte[] under that key; a nested structure was flattened incorrectly so a container landed on a scalar field.","commonSituations":"API contract drift where a scalar field became structured (versioning change on the backend); reusing request maps as scratch storage and leaving a helper object under a numeric key; deserializers that map unknown JSON types into Map/List; unit tests hand-building maps with wrong literal types.","solutions":["Log the class named in the message plus the raw value to identify what actually sits at that key.","If the value is a container, extract the scalar you need explicitly (e.g. from the nested map) instead of pointing getInteger at the wrapper.","Align the map contents with the JSON contract: only Number or numeric String may occupy this key — fix the producer or the test fixture.","Guard the call with an instanceof check (Number || String) and apply your own fallback for other types."],"exampleFix":"// before\nint userId = JSON.getIntValue(req, \"id\"); // throws when \"id\" holds {\"$\": 123}\n\n// after\nObject raw = req.get(\"id\");\nif (raw instanceof Map) { raw = ((Map<?, ?>) raw).get(\"$\"); }\nint userId = raw instanceof Number ? ((Number) raw).intValue() : Integer.parseInt(String.valueOf(raw).trim());","handlingStrategy":"type-guard","validationCode":"Object v = req.get(\"id\");\nif (v != null && !(v instanceof Number) && !(v instanceof String)) {\n    throw new IllegalStateException(\"'id' must be number or numeric string, got \" + v.getClass().getName());\n}","typeGuard":"static boolean isScalarNumberOrString(Object v) {\n    return v == null || v instanceof Number || v instanceof String;\n}","tryCatchPattern":"try {\n    int id = JSON.getIntValue(req, \"id\");\n} catch (IllegalArgumentException e) {\n    log.warn(\"Non-scalar at 'id': {}\", e.getMessage());\n    // unwrap container if that is the real shape, else fail request with 400\n}","preventionTips":["Keep scalar keys scalar: never stash helper objects in request maps under data key names.","Validate payload types at the trust boundary with a schema validator.","Cover schema versions in integration tests so structural drift is caught before release."],"tags":["json","type-conversion","apijson","schema-mismatch"],"backgroundTag":null,"analyzedSha":"5284052872898eddc449a58f629e5c8d588b8e22","analyzedAt":"2026-08-14T15:15:29.577Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}