简体   繁体   中英

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]

Case 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 .

Case 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. So your obj[0] must be an array of objects, too. Edit: Or the other way around: Code 2 should expect an Object instead of Object[].

Why not using variable parameters in Case 2? Like:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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