麻烦大家给我看看下面的这个程序怎么不能运行啊,写的明明和老师的一模一样(Java基础加强第32节),可就是一直报错,
说没有set和get方法,可是明明有啊,所需的两个工具包我也已经导入并添加到了buildpath里面了啊!
import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import org.apache.commons.beanutils.BeanUtils;
public class JavaBean
{
public static void main(String [] args) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException, IntrospectionException, NoSuchMethodException
{
Bean bean=new Bean();
bean.setAge(45);
PropertyDescriptor pd = new PropertyDescriptor("age",bean.getClass());
Method methodGetX = pd.getReadMethod();
Object retVal = methodGetX.invoke(bean);
System.out.println(retVal);
BeanUtils.setProperty(bean, "age","9");//为什么此句话不能执行,抛出NoSuchMethodException
//原句如下:Caused by: java.lang.NoSuchMethodException: Property 'age' has no setter method
System.out.println(BeanUtils.getProperty(bean, "age"));//此句话也不能执行,所没有get方法,可是我明明有啊!
}
}
class Bean
{
private int age;
public int getAge() {//我的get方法不是在这里吗?
return age;
}
public void setAge(int age) {//我的set方法也不是在这里吗?
this.age = age;
}
}
|