chinabugotech/hutool · error · ConvertException
Can not convert {} to {}
Error message
Can not convert {} to {} What it means
EnumConverter.convertInternal tries EnumItem conversion, then a static valueOf-like method on the enum, then Enum.valueOf. If none yields a constant it throws ConvertException with the value and enum class.
Source
Thrown at hutool-core/src/main/java/cn/hutool/core/convert/impl/EnumConverter.java:53
* @param enumClass 转换成的目标Enum类
*/
public EnumConverter(Class enumClass) {
this.enumClass = enumClass;
}
@Override
protected Object convertInternal(Object value) {
Enum enumValue = tryConvertEnum(value, this.enumClass);
if (enumValue == null && !(value instanceof String)) {
// 最后尝试先将value转String,再valueOf转换
enumValue = Enum.valueOf(this.enumClass, convertToStr(value));
}
if (null != enumValue) {
return enumValue;
}
throw new ConvertException("Can not convert {} to {}", value, this.enumClass);
}
@Override
public Class getTargetType() {
return this.enumClass;
}
/**
* 尝试转换,转换规则为:
* <ul>
* <li>如果实现{@link EnumItem}接口,则调用fromInt或fromStr转换</li>
* <li>找到类似转换的静态方法调用实现转换且优先使用</li>
* <li>约定枚举类应该提供 valueOf(String) 和 valueOf(Integer)用于转换</li>
* <li>oriInt /name 转换托底</li>
* </ul>
*
* @param value 被转换的值
* @param enumClass enum类View on GitHub (pinned to 8870454b2a)
Solutions
- Validate the value against EnumUtil.getNames(enumClass) / fromString before converting.
- Pass a defaultValue so AbstractConverter returns it instead of throwing.
- Normalize input (trim, uppercase) to match constant names, or have your enum implement EnumItem for int/string mapping.
Example fix
// before
Color c = Convert.convert(userInput, Color.class);
// after - validate then convert
String name = userInput.trim().toUpperCase();
if (!EnumUtil.getNames(Color.class).contains(name)) {
return Color.DEFAULT;
}
Color c = Convert.convert(name, Color.class); Defensive patterns
Strategy: validation
Validate before calling
if (value instanceof String) {
String name = ((String) value).trim().toUpperCase();
if (!EnumUtil.getNames(enumClass).contains(name)) {
return defaultValue; // or throw a controlled error
}
}
Convert.convert(value, enumClass); Try / catch
try {
return Convert.convert(value, enumClass);
} catch (ConvertException e) {
if (e.getMessage().startsWith("Can not convert") && e.getMessage().contains(enumClass.getSimpleName())) {
return defaultValue;
}
throw e;
} Prevention
- Validate enum input against EnumUtil.getNames / fromString at the boundary.
- Normalize casing (trim + uppercase) before converting.
- Have domain enums implement EnumItem for stable int/str mapping.
When it happens
Trigger: Calling Convert.convert("BLUE", Color.class) when Color has no constant named BLUE; converting an int that matches no enum ordinal/EnumItem int code; case mismatch ("blue" vs "BLUE").
Common situations: User input to an enum where the value does not exist; external data using a different casing or synonym; numeric value out of the enum's range.
Related errors
- Bad number '{}{}' at: {}
- Unknown unit '{}' at: {}
- Video URI is empty
- File not found!
- Base58 checksum is invalid
AI-assisted analysis of chinabugotech/hutool@8870454b2a (2026-08-14).
Data as JSON: /api/errors/475a832b0858bcc7.
Report an issue: GitHub.