本帖最后由 唐辉辉 于 2012-6-24 10:51 编辑
- import org.apache.commons.beanutils.BeanUtils;
- public class BeanUtilsDemo {
- /**
- * @param args
- */
- public static void main(String[] args) throws Exception {
- // TODO Auto-generated method stub
- BeanUtilsData bud = new BeanUtilsData(3, 9);
-
- BeanUtils.setProperty(bud, "y", "19");
- System.out.println(bud.getY());
- //System.out.println(BeanUtils.getProperty(isd, "y"));
- }
- }
- class BeanUtilsData{
-
- private int x;
- private int y;
-
- public BeanUtilsData(int x, int y) {
- super();
- this.x = x;
- this.y = y;
- }
- public int getX() {
- return x;
- }
- public void setX(int x) {
- this.x = x;
- }
- public int getY() {
- return y;
- }
- public void setY(int y) {
- this.y = y;
- }
- }
复制代码 Exception in thread "main" java.lang.reflect.InvocationTargetException: Cannot set y
at org.apache.commons.beanutils.BeanUtilsBean.setProperty(BeanUtilsBean.java:1019)
at org.apache.commons.beanutils.BeanUtils.setProperty(BeanUtils.java:456)
at BeanUtilsDemo.main(BeanUtilsDemo.java:13)
Caused by: java.lang.NoSuchMethodException: Property 'y' has no setter method in class 'class BeanUtilsData'
at org.apache.commons.beanutils.PropertyUtilsBean.setSimpleProperty(PropertyUtilsBean.java:2138)
at org.apache.commons.beanutils.PropertyUtilsBean.setNestedProperty(PropertyUtilsBean.java:1957)
at org.apache.commons.beanutils.PropertyUtilsBean.setProperty(PropertyUtilsBean.java:2064)
at org.apache.commons.beanutils.BeanUtilsBean.setProperty(BeanUtilsBean.java:1017)
... 2 more
报错的地方现在已经找到,需要把JavaBean类修饰为public的。但为什么要修饰为public呢? 有没有朋友知道原因?
|