本帖最后由 肖川 于 2013-12-9 08:42 编辑
- package com.itheima.newtech;
- import java.beans.BeanInfo;
- import java.beans.IntrospectionException;
- import java.beans.Introspector;
- import java.beans.PropertyDescriptor;
- import java.lang.reflect.Method;
- import org.apache.commons.beanutils.BeanUtils;
- import org.apache.commons.beanutils.BeanUtilsBean;
- public class BeanUtilsDemo
- {
- /**
- * @param args
- * @throws Exception
- */
- public static void main(String[] args) throws Exception
- {
- IntroSpectorDemoBean1 isdb = new IntroSpectorDemoBean1(22, "Mike");
-
- String propertyName = "age";
-
- BeanUtils.getProperty(isdb, propertyName);
- }
- }
- class IntroSpectorDemoBean1
- {
- private int age;
- public String name;
-
- public IntroSpectorDemoBean1()
- {
- super();
- }
- public IntroSpectorDemoBean1(int age, String name)
- {
- super();
- this.age = age;
- this.name = name;
- }
- public int getAge()
- {
- return age;
- }
- public void setAge(int age)
- {
- this.age = age;
- }
- public String getName()
- {
- return name;
- }
- public void setName(String name)
- {
- this.name = name;
- }
-
- }
复制代码
报错为:
- Exception in thread "main" java.lang.NoSuchMethodException: Property 'age' has no getter method
- at org.apache.commons.beanutils.PropertyUtilsBean.getSimpleProperty(PropertyUtilsBean.java:1127)
- at org.apache.commons.beanutils.PropertyUtilsBean.getNestedProperty(PropertyUtilsBean.java:686)
- at org.apache.commons.beanutils.BeanUtilsBean.getNestedProperty(BeanUtilsBean.java:698)
- at org.apache.commons.beanutils.BeanUtilsBean.getProperty(BeanUtilsBean.java:723)
- at org.apache.commons.beanutils.BeanUtils.getProperty(BeanUtils.java:265)
- at com.itheima.newtech.BeanUtilsDemo.main(BeanUtilsDemo.java:26)
复制代码
可是我明明有get方法啊。求解答。 |