本帖最后由 杨增坤 于 2013-9-11 17:46 编辑
- import org.apache.commons.beanutils.BeanUtils;
- public class BeanUtilsDemo1
- {
- public static void main(String[] args) throws Exception
- {
- Person p1 = new Person("Ansen", 24);
- BeanUtils.setProperty(p1,"age","23");
- }
- }
- class Person
- {
- private String name ;
- private int age;
- public Person(String name , int age)
- {
- this.name = name ;
- this.age = age;
- }
- public String getName()
- {
- return name;
- }
- public void setName(String name)
- {
- this.name = name;
- }
- public int getAge()
- {
- return age;
- }
- public void setAge(int age)
- {
- this.age = age;
- }
- }
复制代码 Exception in thread "main" java.lang.reflect.InvocationTargetException: Cannot set age
at org.apache.commons.beanutils.BeanUtilsBean.setProperty(BeanUtilsBean.java:1019)
at org.apache.commons.beanutils.BeanUtils.setProperty(BeanUtils.java:456)
at com.JavaBean.BeanUtilsDemo1.main(BeanUtilsDemo1.java:11)
Caused by: java.lang.NoSuchMethodException: Property 'age' has no setter method in class 'class com.JavaBean.Person'
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
以上想set age属性,却抱错了,实在搞不懂哪里错了?
接下来试试get 属性的值,发现还是抱错,抱错信息如下,请大侠帮忙分析一下。。
Exception in thread "main" java.lang.NoSuchMethodException: Property 'name' has no getter method in class 'class com.JavaBean.Person'
at org.apache.commons.beanutils.PropertyUtilsBean.getSimpleProperty(PropertyUtilsBean.java:1327)
at org.apache.commons.beanutils.PropertyUtilsBean.getNestedProperty(PropertyUtilsBean.java:770)
at org.apache.commons.beanutils.BeanUtilsBean.getNestedProperty(BeanUtilsBean.java:715)
at org.apache.commons.beanutils.BeanUtilsBean.getProperty(BeanUtilsBean.java:741)
at org.apache.commons.beanutils.BeanUtils.getProperty(BeanUtils.java:382)
at com.JavaBean.BeanUtilsDemo1.main(BeanUtilsDemo1.java:10)
我使用BeanUtils的Jar包为:commons-beanutils-1.8.3.jar,今天从网上download的
日志jar为:commons-logging-1.1.3.jar
|