Method in the type is not applicable to the arguments

六月ゝ 毕业季﹏ 提交于 2019-12-10 16:16:47

问题


i've looked through many posts on here and couldn't quite see the solution I need...

I'm getting the error:

the method initTimer(untitled.Object, String, int int) in the type untitled.TimerClass is not applicable for the arguments (untitled.Toon, String, int, int)

and it's driving me crazy.

    timers.initTimer(character, "regenAdd", 0,3);

The above line is the one throwing the error and the following is the function:

public void initTimer(final Object obj, final String method, int delay, int period) {
delay*=1000;
period*=1000;

final Class<?> unknown = obj.getClass();

new Timer().schedule(new TimerTask() {
  public void run() {
    try {
      //get the method from the class 
      Method whatToDo = unknown.getMethod(method, null);
      try {
        //invoke() the object method
        whatToDo.invoke(obj);
      } catch(Exception e) {
        println("Exception encountered: " + e);
      }
    } catch(NoSuchMethodException e) {
      println("Exception encountered: " + e);
    }
    runState = getTimerState();

    if (!runState) {
      println("timer dead");
      this.cancel();
    }
  }
}
, delay, period);
}

Thanks in advance to anybody who can help with this :)

Additional info:

runState is a boolean just incase you couldn't guess and character is an instance of the Toon class; the above method is within the TimerClass class and 'timers' is an instance of that class.


回答1:


The error message

the method initTimer(untitled.Object, String, int int) in the type untitled.TimerClass is not applicable for the arguments (untitled.Toon, String, int, int)

is due to the fact that untitled.Toon does not extend untitled.Object. It extends java.lang.Object of course, which is why the reason is not immediately obvious from the source code.




回答2:


Also, another error is initTimer(untitled.Object, String, int) is being called as (untitled.Toon, String, int, int) - notice the difference in number of arguments - 1 int in method declaration and 2 int in calling the method.

Please remember to correct that also.



来源:https://stackoverflow.com/questions/13888663/method-in-the-type-is-not-applicable-to-the-arguments

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!