Question

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

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:

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

Output is 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?

Was it helpful?

Solution

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.

Use

Handshake.class.getMethod("CheckRevision", Session.class);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top