chinabugotech/hutool · error · IllegalArgumentException

Field [{}] not found in Class [{}]

Error message

Field [{}] not found in Class [{}]

What it means

Thrown by FieldComparator constructor when the specified field name does not exist in the given bean class. The getNonNullField() method uses ClassUtil.getDeclaredField() to look up the field and throws if it returns null. Note that getDeclaredField only searches the declared fields of the exact class, not inherited fields from superclasses (depending on ClassUtil's implementation).

Source

Thrown at hutool-core/src/main/java/cn/hutool/core/comparator/FieldComparator.java:63

	 * @param field       字段
	 */
	public FieldComparator(boolean nullGreater, boolean compareSelf, Field field) {
		super(nullGreater, compareSelf, (bean) ->
			(Comparable<?>) ReflectUtil.getFieldValue(bean,
				Assert.notNull(field, "Field must be not null!")));
	}

	/**
	 * 获取字段,附带检查字段不存在的问题。
	 *
	 * @param beanClass Bean类
	 * @param fieldName 字段名
	 * @return 非null字段
	 */
	private static Field getNonNullField(Class<?> beanClass, String fieldName) {
		final Field field = ClassUtil.getDeclaredField(beanClass, fieldName);
		if (field == null) {
			throw new IllegalArgumentException(StrUtil.format("Field [{}] not found in Class [{}]", fieldName, beanClass.getName()));
		}
		return field;
	}
}

View on GitHub (pinned to 8870454b2a)

Solutions

  1. Verify the field name exists in the target class — check for typos and case sensitivity.
  2. If the field is inherited, use ReflectUtil.getField() (which searches superclasses) to confirm the field name, or restructure to use the field from the declaring class.

Example fix

// before
Comparator<MyBean> cmp = new FieldComparator<>(MyBean.class, "usrname"); // typo — throws

// after
Comparator<MyBean> cmp = new FieldComparator<>(MyBean.class, "username"); // correct field name
Defensive patterns

Strategy: validation

Validate before calling

Field field = cn.hutool.core.util.ClassUtil.getDeclaredField(beanClass, fieldName);
if (field == null) {
    throw new IllegalArgumentException("Field " + fieldName + " not found in " + beanClass.getName());
}
Comparator<T> cmp = new FieldComparator<>(beanClass, fieldName);

Type guard

public static boolean fieldExists(Class<?> clazz, String fieldName) {
    return cn.hutool.core.util.ClassUtil.getDeclaredField(clazz, fieldName) != null;
}

Prevention

When it happens

Trigger: Calling new FieldComparator(MyClass.class, "nonExistentField") where the field name does not match any declared field in MyClass. Also triggered by typos in field names, case sensitivity mismatches, or referencing a field from a superclass that getDeclaredField does not traverse.

Common situations: Typos in the field name string. Field renamed during refactoring but the comparator call not updated. Case sensitivity issues (Java field names are case-sensitive). Referencing inherited fields that getDeclaredField does not find. Using a property name (getter-based) instead of the actual field name.

Related errors


AI-assisted analysis of chinabugotech/hutool@8870454b2a (2026-08-14). Data as JSON: /api/errors/5575ed4e49986a69. Report an issue: GitHub.