java.lang.NoSuchMethodException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15575585/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 20:07:30  来源:igfitidea点击:

java.lang.NoSuchMethodException

javanosuchmethod

提问by user2199080

I'm currently coding a Java application, but I got stuck.

我目前正在编写 Java 应用程序,但我被卡住了。

package framework.messages.requests;

import framework.sessions.Session;

public class Handshake {
? ??
? ? public static void CheckRevision(Session Session) {
? ? ? ? try {
? ? ? ? ? ? System.out.println("[" + Session.GetID() + "] --> " + Session.GetRequest().ReadUTF());
? ? ? ? }
? ? ? ? catch (Exception ex) {
? ? ? ? ? ??
? ? ? ? }
? ? }??
}

Is what my class is.

是我的班级。

In main(String[] args)if I use:

main(String[] args)如果我使用:

System.out.println(Handshake.class.getMethods()[0].getName());

Output is CheckRevision.

输出是 CheckRevision。

But if I use somewhere else:

但如果我在其他地方使用:

this.Packets[6428] = Handshake.class.getMethod("CheckRevision");

I get:

我得到:

Exception in thread "main" java.lang.NoSuchMethodException: framework.messages.requests.Handshake.CheckRevision()
    at java.lang.Class.getMethod(Unknown Source)
    at framework.messages.MessageHandler.AddPackets(MessageHandler.java:18)
    at framework.Framework.main(Framework.java:34)

Why don't I get the good method?

为什么我没有得到好的方法?

回答by rgettman

If you supply the getMethodmethod with only the method name, then it will assume that you are looking for a no-argument method. The getMethodmethod has a varargs argument for the classes of the parameters of the method you're looking for.

如果您getMethod只提供方法名称的方法,那么它将假定您正在寻找一个无参数的方法。该getMethod方法有一个 varargs 参数,用于您要查找的方法的参数的类。

Use

利用

Handshake.class.getMethod("CheckRevision", Session.class);