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
java.lang.NoSuchMethodException
提问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 getMethod
method with only the method name, then it will assume that you are looking for a no-argument method. The getMethod
method 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);