1

我得到一个IllegalArgumentException,但我不知道为什么。

我试图访问的功能:

private static Player checkEvents(Player[] players, GameMaster bananas)

有问题的代码:

@Test
public void testCheckEvents() throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
    Game g = new Game();
    GameMaster gm = new GameMaster(4);
    Player[] p = new Player[] {new Player(gm), new Player(gm), new Player(gm), new Player(gm)};

    Method checkEvents = g.getClass().getDeclaredMethod("checkEvents", new Class[] {p.getClass(), GameMaster.class});
    checkEvents.setAccessible(true);

    checkEvents.invoke(p, gm); // fails here
}

失败:

testCheckEvents(nth.bananas.GameTest)
java.lang.IllegalArgumentException: wrong number of arguments

我究竟做错了什么?

4

1 回答 1

4

第一个参数invoke必须是调用方法的对象:

checkEvents.invoke(g, p, gm)

既然你的方法是static,你也可以用null对象引用来代替g

于 2009-12-27T01:11:50.623 回答