chinabugotech/hutool · error · BeanException

No public field or set method for {}

Error message

No public field or set method for {}

What it means

Mirror of error 30 for the set path: DynaBean.set resolves a PropDesc and throws BeanException if no public field or setter exists for the name. Map-backed beans route to Map.put and never hit this. The error means the property is read-only or non-existent on the class.

Source

Thrown at hutool-core/src/main/java/cn/hutool/core/bean/DynaBean.java:158

			return null;
		}
	}

	/**
	 * 设置字段值
	 *
	 * @param fieldName 字段名
	 * @param value     字段值
	 * @throws BeanException 反射获取属性值或字段值导致的异常
	 */
	@SuppressWarnings({"unchecked", "rawtypes"})
	public void set(String fieldName, Object value) throws BeanException {
		if (Map.class.isAssignableFrom(beanClass)) {
			((Map) bean).put(fieldName, value);
		} else {
			final PropDesc prop = BeanUtil.getBeanDesc(beanClass).getProp(fieldName);
			if (null == prop) {
				throw new BeanException("No public field or set method for {}", fieldName);
			}
			prop.setValue(bean, value);
		}
	}

	/**
	 * 执行原始Bean中的方法
	 *
	 * @param methodName 方法名
	 * @param params     参数
	 * @return 执行结果,可能为null
	 */
	public Object invoke(String methodName, Object... params) {
		return ReflectUtil.invoke(this.bean, methodName, params);
	}

	/**
	 * 获得原始Bean

View on GitHub (pinned to 8870454b2a)

Solutions

  1. Cross-check the name against getBeanDesc(...).getProps() filtering for writable properties.
  2. Add a setter or make the field public for properties you intend to set dynamically.
  3. Use a Map-backed DynaBean when you only need to store arbitrary key/values.

Example fix

// before
dynaBean.set("id", value); // id is final, no setter
// after
// add `public void setId(...) { this.id = value; }` to the class, or use a Map-backed DynaBean
Defensive patterns

Strategy: validation

Validate before calling

PropDesc p = BeanUtil.getBeanDesc(bean.getClass()).getProp(name);
if (p == null || p.getSetter() == null && (p.getField()==null||!Modifier.isPublic(p.getField().getModifiers()))) throw new IllegalStateException("no writable property: " + name);

Type guard

static boolean isWritable(BeanClass c, String f) { PropDesc p = BeanUtil.getBeanDesc(c).getProp(f); return p != null && (p.getSetter()!=null || (p.getField()!=null && Modifier.isPublic(p.getField().getModifiers()))); }

Try / catch

try { dynaBean.set(name, value); } catch (BeanException e) { /* skip unwritable */ }

Prevention

When it happens

Trigger: dynaBean.set("readOnlyProp", value) where the field has no setter and is not public; set("missing", ...); setting a final field without a setter.

Common situations: Populating a bean from a map/config that contains keys with no writable counterpart; immutable DTOs with only getters; misaligned column-to-field mappings.

Related errors


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