Java getMethod() results in NoSuchMethodException

2019-09-18 06:19发布

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.

However, I cannot get my code working and I get a NoSuchMethodException error that I do not know how to fix. The lines at the source of the problem are half-way through the Command class (complete classes reproduced at the bottom):

        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

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)
    {   
        [...]
    }

}

1条回答
欢心
2楼-- · 2019-09-18 06:46

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.

For more details see here:

Class.getMethod JavaDoc

查看更多
登录 后发表回答