Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/398.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 反射调用抛出IllegalArgumentException_Java - Fatal编程技术网

Java 反射调用抛出IllegalArgumentException

Java 反射调用抛出IllegalArgumentException,java,Java,我错过了什么 我得到一个异常:java.lang.IllegalArgumentException:对象不是声明类的实例 public boolean onSave(Object entity,Serializable id,Object[] state, String[] propertyNames, Type[] types) { Class<?> clazz=entity.getClass(); System.out.println(" Clazzz

我错过了什么

我得到一个异常:java.lang.IllegalArgumentException:对象不是声明类的实例

public boolean onSave(Object entity,Serializable id,Object[] state, String[] propertyNames, Type[] types) {
    Class<?> clazz=entity.getClass();   
    System.out.println("   Clazzz is:"+clazz);
    Method[] methods = clazz.getMethods();

      for(Method method : methods){
          if(method.getName().startsWith("get") && String.class.equals(method.getReturnType())){
              System.out.println("getter: " + method);

                 try {
                    String s=(String) method.invoke(clazz,(Object[]) null); //java.lang.IllegalArgumentException: object is not an instance of declaring class
                    System.out.println(" value in s is:"+s);
                } catch (IllegalAccessException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
}
public boolean onSave(对象实体、可序列化id、对象[]状态、字符串[]属性名称、类型[]类型){
Class clazz=entity.getClass();
System.out.println(“clazz是:“+clazz”);
方法[]methods=clazz.getMethods();
用于(方法:方法){
if(method.getName().startsWith(“get”)&&String.class.equals(method.getReturnType())){
System.out.println(“getter:+方法);
试一试{
String s=(String)method.invoke(clazz,(Object[])null);//java.lang.IllegalArgumentException:对象不是声明类的实例
System.out.println(“s中的值为:“+s”);
}捕获(非法访问例外e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
}
}
您需要编写

String s=(String) method.invoke(entity, (Object[]) null);
Method.invoke
的第一个参数对于静态方法或应该调用该方法的对象为空。

您需要编写

String s=(String) method.invoke(entity, (Object[]) null);
Method.invoke
的第一个参数对于静态方法或应该调用该方法的对象为空。

您需要编写

String s=(String) method.invoke(entity, (Object[]) null);
Method.invoke
的第一个参数对于静态方法或应该调用该方法的对象为空。

您需要编写

String s=(String) method.invoke(entity, (Object[]) null);
Method.invoke
的第一个参数对于静态方法或应该调用方法的对象为null