简体   繁体   English

通过反射调用方法时出错

[英]Error while invoking method through reflection

Following are the two cases of Methods which I invoke using reflection 以下是我使用反射调用的方法的两种情况

actualoutput = mgenerateouput.invoke(outputclassinst,obj); 

obj is Object array Type which contains Section Type object in obj[0] objObject数组类型,其中包含obj[0]中的Section Type对象

Case 1: 情况1:

public Student[] expectedOutputString(Section sec){
        //Object arra[] = Section.makeSection((String[])params[0]);
        ReportCard rc = new ReportCard();
        Student[] exOut = rc.orderClass(sec);
        return exOut;
    }

it's working in first case perfect but in second case below when i declare parameter type as Object i get IllegalArgumentException . 它在第一种情况下工作完美,但在下面的第二种情况下,当我将参数类型声明为Object我得到了IllegalArgumentException

Case 2: 情况2:

public Student[] expectedOutputString(Object params[]){
    //Object arra[] = Section.makeSection((String[])params[0]);
    ReportCard rc = new ReportCard();
    Student[] exOut = rc.orderClass((Section)params[0]);
    return exOut;
}

[ [

Could be a typo: 可能是拼写错误:

rc.orderClass((Section)params[0]);  // was: Second
               ^^^^^^^

Code 2 does not declare the parameter as Object but as Object[], ie array of objects. 代码2并不将参数声明为Object,而是声明为Object [],即对象数组。 So your obj[0] must be an array of objects, too. 因此,您的obj [0]也必须是对象数组。 Edit: Or the other way around: Code 2 should expect an Object instead of Object[]. 编辑:或者反过来:代码2应该期望使用Object而不是Object []。

Why not using variable parameters in Case 2? 为什么在案例2中不使用可变参数? Like: 喜欢:

public Student[] expectedOutputString(Object ... params){

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM