我尝试使用 setter 设置值,但出现了 null。请帮我解决这个问题,如果还有其他更好的方法可以做。
import org.apache.commons.beanutils.BeanUtils;
public class TestSetter {
public static void main(String args[]) throws Exception
{
Test t = new Test();
BeanUtils.setProperty(t,"te","teval");
System.out.println("tevalue :"+t.getTe());
}
}
class Test
{
String te;
public String getTe() {
return te;
}
public void setTe(String te) {
this.te = te;
}
}
例外 :
Exception in thread "main" java.lang.reflect.InvocationTargetException: Cannot set te
at org.apache.commons.beanutils.BeanUtilsBean.setProperty(BeanUtilsBean.java:1025)
at org.apache.commons.beanutils.BeanUtils.setProperty(BeanUtils.java:313)
at test.reflection.TestSetter.main(TestSetter.java:10)
Caused by: java.lang.NoSuchMethodException: Property 'te' has no setter method
at org.apache.commons.beanutils.PropertyUtilsBean.setSimpleProperty(PropertyUtilsBean.java:1746)
at org.apache.commons.beanutils.PropertyUtilsBean.setNestedProperty(PropertyUtilsBean.java:1648)
at org.apache.commons.beanutils.PropertyUtilsBean.setProperty(PropertyUtilsBean.java:1677)
at org.apache.commons.beanutils.BeanUtilsBean.setProperty(BeanUtilsBean.java:1022)
... 2 more