TheAlgorithms/Java · error · NoSuchElementException

No converter for: {}

Error message

No converter for: {}

What it means

Thrown by UnitsConverter.convert when no conversion path exists between inputUnit and outputUnit. The converter precomputes direct, inverse, and transitive (compositional) conversions at construction time; if the requested ordered pair is not in that precomputed map, computeIfAbsent's loader throws NoSuchElementException.

Source

Thrown at src/main/java/com/thealgorithms/conversions/UnitsConverter.java:136

        units = extractUnits(conversions);
    }

    /**
     * Converts a value from one unit to another.
     *
     * @param inputUnit the unit of the input value.
     * @param outputUnit the unit to convert the value into.
     * @param value the value to convert.
     * @return the converted value in the target unit.
     * @throws IllegalArgumentException if inputUnit equals outputUnit.
     * @throws NoSuchElementException if no conversion exists between the units.
     */
    public double convert(final String inputUnit, final String outputUnit, final double value) {
        if (inputUnit.equals(outputUnit)) {
            throw new IllegalArgumentException("inputUnit must be different from outputUnit.");
        }
        final var conversionKey = Pair.of(inputUnit, outputUnit);
        return conversions.computeIfAbsent(conversionKey, k -> { throw new NoSuchElementException("No converter for: " + k); }).convert(value);
    }

    /**
     * Retrieves the set of all units supported by this converter.
     *
     * @return a set of available units.
     */
    public Set<String> availableUnits() {
        return units;
    }
}

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Check converter.availableUnits() before calling convert to confirm both units are known.
  2. Add the missing basic conversion Pair to the map passed to the constructor so the transitive closure includes the desired path.
  3. Catch NoSuchElementException and report the unsupported pair to the caller with the list of available units.

Example fix

// before
double v = converter.convert("Meters", "Fahrenheit", 100);
// after
if (!converter.availableUnits().containsAll(Set.of(inputUnit, outputUnit))) {
    throw new IllegalArgumentException("Unsupported units. Known: " + converter.availableUnits());
}
double v = converter.convert(inputUnit, outputUnit, value);
Defensive patterns

Strategy: validation

Validate before calling

static double safeConvert(UnitsConverter c, String in, String out, double v) {
    if (!c.availableUnits().containsAll(Set.of(in, out))) {
        throw new IllegalArgumentException("Unknown unit(s). Available: " + c.availableUnits());
    }
    return c.convert(in, out, v);
}

Type guard

static boolean conversionLikelyExists(UnitsConverter c, String in, String out) {
    return c.availableUnits().contains(in) && c.availableUnits().contains(out);
}

Try / catch

try {
    return converter.convert(in, out, value);
} catch (NoSuchElementException e) {
    throw new ApiException(400, "No conversion path between " + in + " and " + out + ". Available: " + converter.availableUnits());
}

Prevention

When it happens

Trigger: Requesting a conversion between two units where no chain of basic conversions connects them (e.g., basic map only defines Celsius<->Fahrenheit and Kelvin<->Celsius, then asking for Fahrenheit<->Kelvin works transitively, but asking for Meters->Fahrenheit fails). Also triggered by typos or units not in the map at all.

Common situations: Adding a new unit to the system without registering conversions to/from existing units; misspelled unit names; assuming the converter auto-discovers unit families it was not told about.

Related errors


AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13). Data as JSON: /api/errors/9e0cbb10355aae38. Report an issue: GitHub.