{"record":{"id":"c9c1fbe719c3da1e","repo":"apache/pulsar","slug":"cannot-convert-from-to-conversion-failed-with","errorCode":null,"errorMessage":"Cannot convert from  to . Conversion failed with ","messagePattern":"Cannot convert from  to \\. Conversion failed with ","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"pulsar-common/src/main/java/org/apache/pulsar/common/util/FieldParser.java","lineNumber":128,"sourceCode":"            EnumResolver r = EnumResolver.constructUsingToString(deserializationConfig, annotatedEnum);\n            T value = (T) r.findEnum((String) from);\n            if (value == null) {\n                throw new RuntimeException(\"Invalid value '\" + from + \"' for enum \" + to);\n            }\n            return value;\n        }\n\n        if (converter == null) {\n            throw new UnsupportedOperationException(\"Cannot convert from \" + from.getClass().getName() + \" to \"\n                    + to.getName() + \". Requested converter does not exist.\");\n        }\n\n        // Convert the value.\n        try {\n            Object val = converter.invoke(to, from);\n            return to.cast(val);\n        } catch (Exception e) {\n            throw new RuntimeException(\"Cannot convert from \" + from.getClass().getName() + \" to \" + to.getName()\n                    + \". Conversion failed with \" + e.getMessage(), e);\n        }\n    }\n\n    /**\n     * Update given Object attribute by reading it from provided map properties.\n     *\n     * @param properties\n     *            which key-value pair of properties to assign those values to given object\n     * @param obj\n     *            object which needs to be updated\n     * @throws IllegalArgumentException\n     *             if the properties key-value contains incorrect value type\n     */\n    public static <T> void update(Map<String, String> properties, T obj) throws IllegalArgumentException {\n        Field[] fields = obj.getClass().getDeclaredFields();\n        Arrays.stream(fields).forEach(f -> {\n            if (properties.containsKey(f.getName())) {","sourceCodeStart":110,"sourceCodeEnd":146,"githubUrl":"https://github.com/apache/pulsar/blob/820761864ed8e2a7d2e52dd9763ad2ae117c1395/pulsar-common/src/main/java/org/apache/pulsar/common/util/FieldParser.java#L110-L146","documentation":"FieldParser.convert() found a registered converter method for the source/target pair, but invoking it via reflection threw. The original exception is wrapped in a RuntimeException whose message includes the converter's own error message (e.g. NumberFormatException text) with the original as the cause.","triggerScenarios":"Calling convert() or update() with a value that fails inside the converter: Integer.valueOf(\"abc\"), Long.valueOf(\"\"), Float.valueOf(\"12.3.4\"), or an enum conversion whose lookup throws.","commonSituations":"Malformed numeric values in a broker/properties config file (e.g. port=\"80o0\"); whitespace or locale-specific decimal separators in numbers; empty strings reaching stringToLong/stringToFloat (which do not null-check unlike stringToInteger).","solutions":["Inspect the cause exception message appended after 'Conversion failed with' to see the actual parse failure (usually NumberFormatException).","Correct the value in the properties/config map so it is a valid literal for the target type.","Pre-validate with a regex or parse try before calling convert/update, and fail with a clearer message naming the offending property.","For float/long empty-string pitfalls, treat blank config values as absent (skip them) rather than passing them through."],"exampleFix":"// before\nint port = FieldParser.convert(\"80o0\", Integer.class); // Conversion failed with For input string: \"80o0\"\n// after\nString raw = \"8080\";\nif (!raw.matches(\"\\\\d+\")) { throw new IllegalArgumentException(\"bad port: \" + raw); }\nint port = FieldParser.convert(raw, Integer.class);","handlingStrategy":"try-catch","validationCode":"static void requireParsableNumber(String v, Class<? extends Number> t) {\n    try {\n        if (t == Integer.class) Integer.valueOf(v.trim());\n        else if (t == Long.class) Long.valueOf(v.trim());\n        else if (t == Double.class) Double.valueOf(v.trim());\n        else if (t == Float.class) Float.valueOf(v.trim());\n    } catch (NumberFormatException e) {\n        throw new IllegalArgumentException(\"value not a \" + t.getSimpleName() + \": \" + v, e);\n    }\n}","typeGuard":"static boolean isParsableInteger(String s) {\n    if (s == null) return false;\n    try { Integer.parseInt(s.trim()); return true; } catch (NumberFormatException e) { return false; }\n}","tryCatchPattern":"try {\n    Integer v = FieldParser.convert(raw, Integer.class);\n} catch (RuntimeException e) {\n    throw new IllegalArgumentException(\"bad config value: \" + raw, e); // message contains the parse error\n}","preventionTips":["Pre-validate numeric config strings with a regex before update()","Skip blank values instead of passing them to convert (stringToLong/stringToFloat don't blank-check)","Keep config files ASCII; avoid locale decimal separators","Log the offending property name when wrapping the failure"],"tags":["reflection","number-format","configuration"],"backgroundTag":"number-format-exception","analyzedSha":"820761864ed8e2a7d2e52dd9763ad2ae117c1395","analyzedAt":"2026-09-06T00:14:20.138Z","contentChangedAt":"2026-09-06T00:14:20.138Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}