inoke setter方法给出异常

时间:2013-09-16 17:37:54

标签: java reflection

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

我有一个bean类“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)

可能是什么问题..?

1 个答案:

答案 0 :(得分:3)

您正在尝试致电

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

此方法采用int