Hi,

I'm trying to call a bunch of methods during run-time using reflection, but I'm getting an exception saying "IllegalArgumentException: wrong number of arguments".

Here's some information on the variables used.
- All elements of mailTestClass[] are classes that extend AbstractTestCase. (FirstTestCase extends AbstractTestCase and FirstTestCase.class is the first element of mailTestClass)
- These classes have constructors which take a String as an argument.
- After instantiating a class, init(Object[][] args) method is called using reflection.

The part of the code which creates the objects using reflection works fine. The code blows up when the method init(Object[][] args) is called.

private void runAll(Class[] mailTestClass, Object[][] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, InstantiationException {
        AbstractTestCase mailTest[] = new AbstractTestCase[mailTestClass.length];
        Method methodInit;
        Constructor mailTestConstructor;
        Class[] constructorParamsClass = new Class[] {String.class};
        Integer count[] = new Integer[mailTestClass.length];
        Object[] constructorParams = new Object[] {"DummyString"};
        int i;
        for (i=0; i<mailTestClass.length; i++)
        {
            try
            {
                mailTestConstructor = mailTestClass[i].getConstructor(constructorParamsClass);
                mailTest[i] = (AbstractTestCase) mailTestConstructor.newInstance(constructorParams);
                methodInit = mailTestClass[i].getDeclaredMethod("init", new Class[] {Object[][].class});
                methodInit.invoke(mailTest[i], args);//throws the exception here

The code till line 14 creates an instance of FirstTestCase passing a String "DummyString" to it's constructor.
In line 15, please check if the way the method is invoked is correct. That's the line I'm not sure of.

In FirstTestCase.java:

public void init ()
    {
      .
      .
      .
    }

    public void init (Object[][] args) // Overloaded methods
    {
        .
        .
        .
    }

Thanks. Let me know if additional information is needed.

Maybe it's this:
The arg should be a [][] array - which is what the variable args contains.
The invoke method requires an array that contains all the parameters for the method being invoked. The init method has 1 parameter, so the invoke method should pass an array of 1 element, which, in turn, should be your [][] array args. Ie, you need to enclose your [][] args parameter in a new 1 element array.

commented: Thanks, dude. +2

Maybe it's this:
The arg should be a [][] array - which is what the variable args contains.
The invoke method requires an array that contains all the parameters for the method being invoked. The init method has 1 parameter, so the invoke method should pass an array of 1 element, which, in turn, should be your [][] array args. Ie, you need to enclose your [][] args parameter in a new 1 element array.

I replaced the line methodInit.invoke(mailTest[i], args); with methodInit.invoke(mailTest[i], new Object[]{args}); and now it's working fine. Thanks, JC.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.