我在使用BeanUtils工具类时抛出了异常,麻烦看一下是什么原因?
代码如下:
package cn.itcast.introspector;
import java.lang.reflect.InvocationTargetException;
import java.util.Date;
/*
* BeanUtils和PropertyUtils工具类的应用
*/
import org.apache.commons.beanutils.*;
public class BeanUtilsTest {
public static void main(String [] args) throws Exception
{
Person1 p = new Person1();
BeanUtils.setProperty(p, "age","12" );
String retVal = BeanUtils.getProperty(p, "age");
System.out.println(retVal);
}
}
//一个标准的javabean类
class Person1{
private String name;
private int age;
private Date birthday;
private int y;
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;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}
导入第三方jar包
我已经成功导入第三方jar包,为甚么还是会抛出异常?
异常信息:
Exception in thread "main" java.lang.reflect.InvocationTargetException: Cannot set age
at org.apache.commons.beanutils.BeanUtilsBean.setProperty(BeanUtilsBean.java:1017)
at org.apache.commons.beanutils.BeanUtils.setProperty(BeanUtils.java:456)
at cn.itcast.introspector.BeanUtilsTest.main(BeanUtilsTest.java:14)
Caused by: java.lang.NoSuchMethodException: Property 'age' has no setter method in class 'class cn.itcast.introspector.Person1'
at org.apache.commons.beanutils.PropertyUtilsBean.setSimpleProperty(PropertyUtilsBean.java:2128)
at org.apache.commons.beanutils.PropertyUtilsBean.setNestedProperty(PropertyUtilsBean.java:1948)
at org.apache.commons.beanutils.PropertyUtilsBean.setProperty(PropertyUtilsBean.java:2054)
at org.apache.commons.beanutils.BeanUtilsBean.setProperty(BeanUtilsBean.java:1015)
... 2 more
|
-
1.jpg
(9.4 KB, 下载次数: 55)
导入的Jar包
|