apache/pulsar · error · RuntimeException

Cannot convert from to . Conversion failed with

Error message

Cannot convert from  to . Conversion failed with 

What it means

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.

Source

Thrown at pulsar-common/src/main/java/org/apache/pulsar/common/util/FieldParser.java:128

            EnumResolver r = EnumResolver.constructUsingToString(deserializationConfig, annotatedEnum);
            T value = (T) r.findEnum((String) from);
            if (value == null) {
                throw new RuntimeException("Invalid value '" + from + "' for enum " + to);
            }
            return value;
        }

        if (converter == null) {
            throw new UnsupportedOperationException("Cannot convert from " + from.getClass().getName() + " to "
                    + to.getName() + ". Requested converter does not exist.");
        }

        // Convert the value.
        try {
            Object val = converter.invoke(to, from);
            return to.cast(val);
        } catch (Exception e) {
            throw new RuntimeException("Cannot convert from " + from.getClass().getName() + " to " + to.getName()
                    + ". Conversion failed with " + e.getMessage(), e);
        }
    }

    /**
     * Update given Object attribute by reading it from provided map properties.
     *
     * @param properties
     *            which key-value pair of properties to assign those values to given object
     * @param obj
     *            object which needs to be updated
     * @throws IllegalArgumentException
     *             if the properties key-value contains incorrect value type
     */
    public static <T> void update(Map<String, String> properties, T obj) throws IllegalArgumentException {
        Field[] fields = obj.getClass().getDeclaredFields();
        Arrays.stream(fields).forEach(f -> {
            if (properties.containsKey(f.getName())) {

View on GitHub (pinned to 820761864e)

Solutions

  1. Inspect the cause exception message appended after 'Conversion failed with' to see the actual parse failure (usually NumberFormatException).
  2. Correct the value in the properties/config map so it is a valid literal for the target type.
  3. Pre-validate with a regex or parse try before calling convert/update, and fail with a clearer message naming the offending property.
  4. For float/long empty-string pitfalls, treat blank config values as absent (skip them) rather than passing them through.

Example fix

// before
int port = FieldParser.convert("80o0", Integer.class); // Conversion failed with For input string: "80o0"
// after
String raw = "8080";
if (!raw.matches("\\d+")) { throw new IllegalArgumentException("bad port: " + raw); }
int port = FieldParser.convert(raw, Integer.class);
Defensive patterns

Strategy: try-catch

Validate before calling

static void requireParsableNumber(String v, Class<? extends Number> t) {
    try {
        if (t == Integer.class) Integer.valueOf(v.trim());
        else if (t == Long.class) Long.valueOf(v.trim());
        else if (t == Double.class) Double.valueOf(v.trim());
        else if (t == Float.class) Float.valueOf(v.trim());
    } catch (NumberFormatException e) {
        throw new IllegalArgumentException("value not a " + t.getSimpleName() + ": " + v, e);
    }
}

Type guard

static boolean isParsableInteger(String s) {
    if (s == null) return false;
    try { Integer.parseInt(s.trim()); return true; } catch (NumberFormatException e) { return false; }
}

Try / catch

try {
    Integer v = FieldParser.convert(raw, Integer.class);
} catch (RuntimeException e) {
    throw new IllegalArgumentException("bad config value: " + raw, e); // message contains the parse error
}

Prevention

When it happens

Trigger: 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.

Common situations: 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).

Related errors


AI-assisted analysis of apache/pulsar@820761864e (2026-09-06). Data as JSON: /api/errors/c9c1fbe719c3da1e. Report an issue: GitHub.