【问题标题】:java.lang.NoSuchMethodExceptionjava.lang.NoSuchMethodException
【发布时间】:2013-03-12 14:51:31
【问题描述】:

我目前正在编写一个 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) {
            
        }
    }  
}

这就是我的课。

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

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

输出为 CheckRevision。

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

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

我明白了:

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)

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

【问题讨论】:

  • 如果你继续为变量使用大写命名,你会遇到很多问题。
  • 为什么我会有很多问题?
  • 因为它不遵循标准的 java 命名约定。
  • Session.GetID() GetID()Session 类上的静态方法还是实例 Session 上的实例方法。

标签: java nosuchmethod


【解决方案1】:

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

使用

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

【讨论】:

    猜你喜欢
    • 2013-11-23
    • 2015-02-25
    • 1970-01-01
    • 1970-01-01
    • 2013-08-04
    • 2020-10-21
    • 2017-03-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多