找到你要的答案

Q:Calling a method from another class with the getMethod gives an error

Q:调用一个方法从另一个类的方法给出了一个错误

So, as the title describes, I am in need of help with calling a method from a class to another. So, to explain a bit further: I'm storing classes in a HashSet. Now I'm trying to access a method located in that class chosen in that HashSet, but it rather gives me that error:

java.lang.IllegalArgumentException: object is not an instance of declaring class
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source).

Code:

 Class<Task> neededClass = taskHandler.getTaskClass("NodeMovementTask");
                try {
                    neededClass.getMethod("addToQueue", Timeline.class, int.class).invoke(timeline, cycleCount);
                } catch (IllegalAccessException | IllegalArgumentException
                        | InvocationTargetException | NoSuchMethodException
                        | SecurityException e) {
                    e.printStackTrace();
                }

Thats the getTaskClass method

public Class<Task> getTaskClass(String classToSearch) {
    Class<Task> returningClass = null;
    for(Class<Task> foundClass : tasks) {
        if(foundClass.getName().equalsIgnoreCase("packages." + classToSearch)) {
            System.out.println("Found!");
            return returningClass = foundClass;
        }
    }
    return returningClass;
}

因此,正如标题所描述的,我需要帮助从一个类调用另一个类的方法。因此,进一步说明一点:我在一个好的存储类。现在我试图访问的方法,选择位于HashSet类,但它给我的错误:

java.lang.IllegalArgumentException: object is not an instance of declaring class
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source).

代码:

 Class<Task> neededClass = taskHandler.getTaskClass("NodeMovementTask");
                try {
                    neededClass.getMethod("addToQueue", Timeline.class, int.class).invoke(timeline, cycleCount);
                } catch (IllegalAccessException | IllegalArgumentException
                        | InvocationTargetException | NoSuchMethodException
                        | SecurityException e) {
                    e.printStackTrace();
                }

这是gettaskclass方法

public Class<Task> getTaskClass(String classToSearch) {
    Class<Task> returningClass = null;
    for(Class<Task> foundClass : tasks) {
        if(foundClass.getName().equalsIgnoreCase("packages." + classToSearch)) {
            System.out.println("Found!");
            return returningClass = foundClass;
        }
    }
    return returningClass;
}
answer1: 回答1:

The first argument to the invoke method is the object instance you want to invoke the method on. If the method is static you can simply supply null as that first argument. Otherwise you need to supply the instance that you actually want to call the method on:

neededClass.getMethod("addToQueue", Timeline.class, int.class)
     .invoke(instance, timeline, cycleCount);
             ^^^^^^^^

调用方法的第一个参数是要调用该方法的对象实例。如果方法是静态的,你可以简单地提供空作为第一个参数。否则,您需要提供实际调用该方法的实例:

neededClass.getMethod("addToQueue", Timeline.class, int.class)
     .invoke(instance, timeline, cycleCount);
             ^^^^^^^^
java  reflection  hashset