开发者

Java: Illegal Argument Exception

开发者 https://www.devze.com 2022-12-15 01:25 出处:网络
I\'m getting an IllegalArgumentException, but I can\'t figure out why. The function I\'m trying to access:

I'm getting an IllegalArgumentException, but I can't figure out why.

The function I'm trying to access:

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

The problematic code:

@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
}

The failure:

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

What am I doing wrong?


The first argument to invoke must be an object on which to invoke the method:

checkEvents.invoke(g, p, gm)

Since your method is static, you can also use null instead of the object reference g.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号