简体   繁体   English

Java getMethod()导致NoSuchMethodException错误

[英]Java getMethod() results in NoSuchMethodException error

I am trying to use reflection to launch the appropriate method when the user inputs a string command. 我试图在用户输入字符串命令时使用反射来启动适当的方法。 For instance, if the user inputs "go" in the terminal, the go() method of the Player class will be called by the process() method of the Command class. 例如,如果用户在终端中输入“go”,则Player类的go()方法将由Command类的process()方法调用。

However, I cannot get my code working and I get a NoSuchMethodException error that I do not know how to fix. 但是,我无法使我的代码工作,并且我得到NoSuchMethodException错误,我不知道如何修复。 The lines at the source of the problem are half-way through the Command class (complete classes reproduced at the bottom): 问题根源的行是Command类的一半(在底部复制完整的类):

        try {
            Method method = pClass.getMethod(commandWord);
            method.invoke(player, this);
        }
        catch (NoSuchMethodException err1) {
            System.out.println("No such method");
        }

Could anyone please guide me? 有人可以指导我吗? I thank you in advance. 我提前谢谢你。

LC LC

Command class: 命令类:

import java.util.ArrayList;
import java.lang.reflect.*;

public class Command
{

    private String commandWord;
    private String secondWord;

    public Command(String firstWord, String secondWord)
    {
        commandWord = firstWord;
        this.secondWord = secondWord;
    }

    public boolean process(Player player) 
    {
        ArrayList<String> validCommands = new ArrayList<String>();
        String methodName;
        int index;

        Class pClass = player.getClass();
        Method[] methods = pClass.getDeclaredMethods();
        for (int i = 0; i < methods.length; i++) {
            if (Modifier.isPublic(methods[i].getModifiers())) {
                validCommands.add(methods[i].getName());
            }
        }

        boolean wantToQuit = false;

        if(commandWord == null) {
            System.out.println("I don't know what you mean...");
            return false;
        }

        if (commandWord.equals("help")) {
            System.out.println("You are lost. You are alone. You wander");
            System.out.println("around at the university.");
            System.out.println();
            System.out.println("Your command words are:");

            for(String command: validCommands) {
                System.out.print(command + "  ");
            }
            System.out.println();
        }
        else if (commandWord.equals("quit")) {
            wantToQuit = quit();
        }

        //THIS IS WHERE I GET THE ERROR
        try {
            Method method = pClass.getMethod(commandWord);
            method.invoke(player, this);
        }
        catch (NoSuchMethodException err1) {
            System.out.println("No such method");
        }
        catch (IllegalAccessException err2) {
            System.out.println("Illegal access");
        }
        catch (InvocationTargetException err3) {
            System.out.println("Illegal access");
        }

        return wantToQuit;
    }

    [...] //some methods omitted
}

Player class: 玩家类:

public class Player
{
    private String name;
    private Room currentRoom;
    private ArrayList<Item> items;

    Player (String name, Room startingRoom)
    {
        this.name = name;
        items = new ArrayList<Item>();
        this.currentRoom = startingRoom;
        printWelcome();
    }

    public void engage()
    {
        [...]
    }

    public void trade(Command command) 
    {
        [...]       }

    public void goRoom(Command command) 
    {   
        [...]       }

    public void search(Command command) 
    {
        [...]       }

    public void takeItem(Command command) 
    {
        [...]       }

    public void dropItem(Command command) 
    {
        [...]       }

    public void lock(Command command)
    {   
        [...]       }

    public void unlock(Command command)
    {   
        [...]
    }

}

Try this. 尝试这个。

pClass.getMethod(commandWord, Command.class)

Seems to me the problem is that you're looking for methods with no 在我看来问题是你正在寻找没有的方法
parameters while these methods all seem to have a parameter of type Command . 参数虽然这些方法似乎都有一个Command类型的参数。

For more details see here: 有关详细信息,请参阅此处

Class.getMethod JavaDoc Class.getMethod JavaDoc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM