{"record":{"id":"c5acdb7b623cd5fd","repo":"Tencent/APIJSON","slug":"cannot-convert-value-of-type-value-getclass-c5acdb","errorCode":null,"errorMessage":"Cannot convert value of type \" + value.getClass().getName() + \" to double","messagePattern":"Cannot convert value of type \" \\+ value\\.getClass\\(\\)\\.getName\\(\\) \\+ \" to double","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"APIJSONORM/src/main/java/apijson/JSON.java","lineNumber":501,"sourceCode":"\tpublic static Float getFloat(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).floatValue();\n\t\t}\n\n\t\tif (value instanceof String) {\n\t\t\ttry {\n\t\t\t\treturn Float.parseFloat((String) value);\n\t\t\t} catch (NumberFormatException e) {\n\t\t\t\tthrow new IllegalArgumentException(\"Cannot convert String value '\" + value + \"' to double: \" + e.getMessage());\n\t\t\t}\n\t\t}\n\n\t\tthrow new IllegalArgumentException(\"Cannot convert value of type \" + value.getClass().getName() + \" to double\");\n\t}\n\n\t/**\n\t * Get a double value from a Map\n\t * @param map Source map\n\t * @param key The key\n\t * @return The double value\n\t * @throws IllegalArgumentException If value cannot be converted to double\n\t */\n\tpublic static float getFloatValue(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).floatValue();\n\t\t}","sourceCodeStart":483,"sourceCodeEnd":519,"githubUrl":"https://github.com/Tencent/APIJSON/blob/5284052872898eddc449a58f629e5c8d588b8e22/APIJSONORM/src/main/java/apijson/JSON.java#L483-L519","documentation":"Thrown by apijson.JSON.getFloat(Map, String) (APIJSONORM/src/main/java/apijson/JSON.java:501) when the value is neither Number nor String (Map, List, Boolean, ...). The message says \"to double\" although the conversion target is float — a copy-paste artifact; the reported class name is accurate and diagnostic.","triggerScenarios":"A float field holds a nested object/array after schema drift; a Boolean where a numeric flag was expected; an internal Java object (Date, Enum, byte[]) stored in the map under that key; the wrong key name used.","commonSituations":"API versioning restructuring scalar fields; middleware attaching objects to shared maps; heterogeneous tenant payloads; test fixtures with wrong literal types; confusion from the \"double\" wording sending developers to inspect getDouble code paths.","solutions":["Trust the class name in the message; log the raw value at that key to confirm the structural mismatch.","Read the scalar from the right key or unwrap the container before converting.","Fix the producer/middleware so only Number or numeric String occupies the key.","Guard with instanceof (Number || String) and handle other types explicitly."],"exampleFix":"// before\nFloat rate = JSON.getFloat(cfg, \"rate\"); // throws when \"rate\" holds {\"value\": 0.5}\n\n// after\nObject raw = cfg.get(\"rate\");\nif (raw instanceof Map) { raw = ((Map<?, ?>) raw).get(\"value\"); }\nFloat rate = raw instanceof Number ? ((Number) raw).floatValue() : null;","handlingStrategy":"type-guard","validationCode":"Object v = cfg.get(\"rate\");\nif (v != null && !(v instanceof Number) && !(v instanceof String)) {\n    throw new IllegalStateException(\"'rate' must be number or numeric string, got \" + v.getClass().getName());\n}","typeGuard":"static boolean isNumberOrString(Object v) {\n    return v == null || v instanceof Number || v instanceof String;\n}","tryCatchPattern":"try {\n    Float rate = JSON.getFloat(cfg, \"rate\");\n} catch (IllegalArgumentException e) {\n    log.warn(\"Wrong type at 'rate' (message says 'double' — known wording bug): {}\", e.getMessage());\n    // unwrap or reject\n}","preventionTips":["Note the 'double' wording bug in getFloat messages; diagnose by the printed class name.","Guard against container values on scalar keys.","Pin payload shapes with contract tests."],"tags":["json","type-conversion","apijson","schema-mismatch","error-message"],"backgroundTag":null,"analyzedSha":"5284052872898eddc449a58f629e5c8d588b8e22","analyzedAt":"2026-08-14T15:15:29.577Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}