{"record":{"id":"0c9ad046489bde27","repo":"chinabugotech/hutool","slug":"no-public-field-or-get-method-for","errorCode":null,"errorMessage":"No public field or get method for {}","messagePattern":"No public field or get method for (.+?)","errorType":"validation","errorClass":"BeanException","httpStatus":null,"severity":"error","filePath":"hutool-core/src/main/java/cn/hutool/core/bean/DynaBean.java","lineNumber":107,"sourceCode":"\t}\n\t//------------------------------------------------------------------------ Constructor end\n\n\t/**\n\t * 获得字段对应值\n\t *\n\t * @param <T>       属性值类型\n\t * @param fieldName 字段名\n\t * @return 字段值\n\t * @throws BeanException 反射获取属性值或字段值导致的异常\n\t */\n\t@SuppressWarnings(\"unchecked\")\n\tpublic <T> T get(String fieldName) throws BeanException {\n\t\tif (Map.class.isAssignableFrom(beanClass)) {\n\t\t\treturn (T) ((Map<?, ?>) bean).get(fieldName);\n\t\t} else {\n\t\t\tfinal PropDesc prop = BeanUtil.getBeanDesc(beanClass).getProp(fieldName);\n\t\t\tif (null == prop) {\n\t\t\t\tthrow new BeanException(\"No public field or get method for {}\", fieldName);\n\t\t\t}\n\t\t\treturn (T) prop.getValue(bean);\n\t\t}\n\t}\n\n\t/**\n\t * 检查是否有指定名称的bean属性\n\t *\n\t * @param fieldName 字段名\n\t * @return 是否有bean属性\n\t * @since 5.4.2\n\t */\n\tpublic boolean containsProp(String fieldName) {\n\t\tif (Map.class.isAssignableFrom(beanClass)) {\n\t\t\treturn ((Map<?, ?>) bean).containsKey(fieldName);\n\t\t} else{\n\t\t\treturn null != BeanUtil.getBeanDesc(beanClass).getProp(fieldName);\n\t\t}","sourceCodeStart":89,"sourceCodeEnd":125,"githubUrl":"https://github.com/chinabugotech/hutool/blob/8870454b2a0c29cc6ffd31dcf5667c8ceb2fc442/hutool-core/src/main/java/cn/hutool/core/bean/DynaBean.java#L89-L125","documentation":"DynaBean.get looks up a PropDesc for the requested field on the wrapped bean's class. If no public field or getter exists with that name, getProp returns null and a BeanException is thrown. Map-backed DynaBeans bypass this (they read from the map), so the error is specific to POJO/class-backed beans.","triggerScenarios":"dynaBean.get(\"nonexistent\"), get(\"privateField\") where the field is not public and has no getter, or a typo'd field name on a class-backed DynaBean.","commonSituations":"Dynamic access driven by external/config-supplied field names; accessing inherited protected fields; mismatch between JSON keys and bean property names.","solutions":["Verify the property name against BeanUtil.getBeanDesc(beanClass).getProps() before calling get.","Ensure the target field is either public or exposed via a getter.","If using a Map-backed source, wrap a Map in DynaBean to avoid property resolution entirely."],"exampleFix":"// before\nObject v = dynaBean.get(\"usrName\"); // typo\n// after\nString field = BeanUtil.getBeanDesc(bean.getClass()).getProps().stream()\n    .map(PropDesc::getFieldName).filter(\"userName\"::equals).findFirst().orElseThrow();\nObject v = dynaBean.get(field);","handlingStrategy":"validation","validationCode":"if (!BeanUtil.getBeanDesc(bean.getClass()).getProps().stream().map(PropDesc::getFieldName).collect(Collectors.toSet()).contains(name)) throw new IllegalStateException(\"no readable property: \" + name);","typeGuard":"static boolean hasReadable(BeanClass c, String f) { return BeanUtil.getBeanDesc(c).getProp(f) != null; }","tryCatchPattern":"try { return dynaBean.get(name); } catch (BeanException e) { return null; }","preventionTips":["Validate field names against getBeanDesc before dynamic get.","Use Map-backed DynaBean for arbitrary key access."],"tags":["bean","dynabean","reflection"],"backgroundTag":null,"analyzedSha":"8870454b2a0c29cc6ffd31dcf5667c8ceb2fc442","analyzedAt":"2026-08-14T04:01:12.892Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}