- package sheng.java.language;
- class Person {
- private String name;
- private int 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;
- }
- public Person(String name, int age) {
- super();
- this.name = name;
- this.age = age;
- }
- public Person() {
- // TODO Auto-generated constructor stub
- }
- }
复制代码- package sheng.java.language;
- import java.lang.reflect.InvocationTargetException;
- import org.apache.commons.beanutils.BeanUtils;
- public class BeanUtilsDemo {
- /**
- * @param args
- * @throws NoSuchMethodException
- * @throws InvocationTargetException
- * @throws IllegalAccessException
- */
- public static void main(String[] args) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
- // TODO Auto-generated method stub
- Person p = new Person("zhansan", 23);
- String name = "age";
-
- System.out.println(BeanUtils.getProperty(p, name));
- }
- }
复制代码 Exception in thread "main" java.lang.NoSuchMethodException: Property 'age' has no getter method in class 'class sheng.java.language.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 sheng.java.language.BeanUtilsDemo.main(BeanUtilsDemo.java:20)
查明原因后发现Person类必须是public.
|
|