Search code examples
javaloopsmethods

Exception in thread "main" java.lang.IllegalArgumentException: wrong number of arguments


I am facing this problem, I am trying to loop all my methods from one class to another and I did do that, but I have 2 loops where one gets executed from the end to the beginning and then the other one does not get executed. This error shows to me

Exception in thread "main" java.lang.IllegalArgumentException: wrong number of arguments

This is where I am trying to get the result of my methods

public class finalResult {
    public static void main(String[] args) throws InvocationTargetException, IllegalAccessException {
        loopUserDataClass();
        loopBankDataClass();
    }

    public static void loopUserDataClass() throws InvocationTargetException, IllegalAccessException {
        userData us = new userData();
        Class<?> getUserDataClass = us.getClass();
        Method[] methods1 = getUserDataClass.getMethods();
        for (Method method : methods1) {
            if (Modifier.isPublic(method.getModifiers())) {
                method.invoke(us, null);
            }
        }
    }
    public static void loopBankDataClass() throws InvocationTargetException, IllegalAccessException {
        bankData bd = new bankData();
        Class<?> getBankClass = bd.getClass();
        Method[] methods = getBankClass.getMethods();
        for (Method method : methods) {
            if (Modifier.isPublic(method.getModifiers())) {
                try {
                    method.invoke(bd, null);
                } catch (Exception ex) {
                    // Handle the exception here
                    ex.printStackTrace();
                }
            }
        }
    }
    }

And here is the class where I have my methods.

public class bankData {
    public double moneyOnAccount = 123.50;
    public String currencyName = "currencyName";
    public boolean accountState = true;
    public String accountId ="2432432423ffsdf";
    public void getMoneyOnAccount() {
        System.out.println("Money on account: " + this.moneyOnAccount);
    }
    public void getCurrencyName()
    {
        System.out.println("Currency: " + this.currencyName);
    }
    public void getAccountState()
    {
        System.out.println("Account statement: " + this.accountState);
    }
    public void getAccountId()
    {
        System.out.println("Account ID: " + this.accountId);
    }
}

I believe that I have a problem with this line

 method.invoke(bd, null);

I tried to put it in too many different ways but always shows me the same error. I do not have any arguments to pass so I do not know what I have to pass there.


Solution

  • So, if I modify your code slightly to do...

    try {
        method.invoke(bd, null);
    } catch (Exception ex) {
        // Handle the exception here
        //ex.printStackTrace();
        System.out.println(" >> " + method.getName());
    }
    

    We can get an idea of which methods are causing your issues. In my testing this prints

     >> wait
     >> wait
     >> wait
     >> equals
     >> notify
     >> notifyAll
    

    Okay, that actually makes sense, all of those methods will expect one or more parameters.

    So, how do we fix it? Well, instead of using Method[] methods = getBankClass.getMethods(); you should probably be using Method[] methods = getBankClass. getDeclaredMethods();

    The difference between the two can be found in the JavaDocs

    Class#getMethods:

    Returns an array containing Method objects reflecting all the public methods of the class or interface represented by this Class object, including those declared by the class or interface and those inherited from superclasses and superinterfaces.

    Class#getDeclaredMethods

    Returns an array containing Method objects reflecting all the declared methods of the class or interface represented by this Class object, including public, protected, default (package) access, and private methods, but excluding inherited methods.