0

我尝试在堆栈溢出和其他站点中搜索。但是,我无法得到答案,这就是我在这里发帖的原因。

我参加了一个豆类“BeanEx”

public class BeanEx {
    private String queryCode;

    private String itemId;

    private int infoId;


    public String getQueryCode() {
        return queryCode;
    }

    public void setQueryCode(String queryCode) {
        this.queryCode = queryCode;
    }

    public String getItemId(){
        return itemId;
    }
    public void setItemId(String itemId){
        this.itemId = itemId;
    }

    public int getInfoId() {
        return infoId;
    }

    public void setInfoId(int infoId) {
        this.infoId = infoId;
    }
}

我的主要方法java文件

BeanEx myClass = new BeanEx();
Class<?> objClass = myClass.getClass();
BeanInfo info = Introspector.getBeanInfo(objClass);
PropertyDescriptor[] props = info.getPropertyDescriptors();
MethodDescriptor[] methodDescriptors = info.getMethodDescriptors();
Map<String, String> beanMap = new HashMap<String, String>();

for (PropertyDescriptor pd : props) {
    System.out.println(pd.getName());
    System.out.println(pd.getReadMethod().getName());
    try {
        System.out.println("hihi = " + pd.getWriteMethod().getName());
        pd.getWriteMethod().invoke(objClass.newInstance(),
                new Object[] { "praee" });
    } catch (NullPointerException npe) {
        System.out.println("ignoring exception");
    }
    // return pd.getName();
}

我得到了例外......

class
getClass
ignoring exception
infoId
getInfoId
hihi = setInfoId
java.lang.IllegalArgumentException: argument type mismatch
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at javaapplication1.RegularExpressionExample.getFieldName(RegularExpressionExample.java:44)
    at javaapplication1.RegularExpressionExample.main(RegularExpressionExample.java:23)

可能是什么问题..?

4

1 回答 1

3

你正试图打电话

new BeanEx().setInfoId("hello");

当这个方法需要一个int

于 2013-09-16T17:40:42.443 回答