{"record":{"id":"5575ed4e49986a69","repo":"chinabugotech/hutool","slug":"field-not-found-in-class","errorCode":null,"errorMessage":"Field [{}] not found in Class [{}]","messagePattern":"Field \\[(.+?)\\] not found in Class \\[(.+?)\\]","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"hutool-core/src/main/java/cn/hutool/core/comparator/FieldComparator.java","lineNumber":63,"sourceCode":"\t * @param field       字段\n\t */\n\tpublic FieldComparator(boolean nullGreater, boolean compareSelf, Field field) {\n\t\tsuper(nullGreater, compareSelf, (bean) ->\n\t\t\t(Comparable<?>) ReflectUtil.getFieldValue(bean,\n\t\t\t\tAssert.notNull(field, \"Field must be not null!\")));\n\t}\n\n\t/**\n\t * 获取字段，附带检查字段不存在的问题。\n\t *\n\t * @param beanClass Bean类\n\t * @param fieldName 字段名\n\t * @return 非null字段\n\t */\n\tprivate static Field getNonNullField(Class<?> beanClass, String fieldName) {\n\t\tfinal Field field = ClassUtil.getDeclaredField(beanClass, fieldName);\n\t\tif (field == null) {\n\t\t\tthrow new IllegalArgumentException(StrUtil.format(\"Field [{}] not found in Class [{}]\", fieldName, beanClass.getName()));\n\t\t}\n\t\treturn field;\n\t}\n}\n","sourceCodeStart":45,"sourceCodeEnd":68,"githubUrl":"https://github.com/chinabugotech/hutool/blob/8870454b2a0c29cc6ffd31dcf5667c8ceb2fc442/hutool-core/src/main/java/cn/hutool/core/comparator/FieldComparator.java#L45-L68","documentation":"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).","triggerScenarios":"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.","commonSituations":"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.","solutions":["Verify the field name exists in the target class — check for typos and case sensitivity.","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."],"exampleFix":"// before\nComparator<MyBean> cmp = new FieldComparator<>(MyBean.class, \"usrname\"); // typo — throws\n\n// after\nComparator<MyBean> cmp = new FieldComparator<>(MyBean.class, \"username\"); // correct field name","handlingStrategy":"validation","validationCode":"Field field = cn.hutool.core.util.ClassUtil.getDeclaredField(beanClass, fieldName);\nif (field == null) {\n    throw new IllegalArgumentException(\"Field \" + fieldName + \" not found in \" + beanClass.getName());\n}\nComparator<T> cmp = new FieldComparator<>(beanClass, fieldName);","typeGuard":"public static boolean fieldExists(Class<?> clazz, String fieldName) {\n    return cn.hutool.core.util.ClassUtil.getDeclaredField(clazz, fieldName) != null;\n}","tryCatchPattern":null,"preventionTips":["Verify field names match the actual declared field exactly (case-sensitive).","Use ClassUtil.getDeclaredField() to check existence before constructing FieldComparator.","Keep field name constants in sync with class definitions.","If using inherited fields, verify ClassUtil.getDeclaredField traverses the hierarchy."],"tags":["comparator","reflection","field-not-found","bean","sort"],"backgroundTag":null,"analyzedSha":"8870454b2a0c29cc6ffd31dcf5667c8ceb2fc442","analyzedAt":"2026-08-14T04:01:12.892Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}