質問

私は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

私は間違って何をしているのですか?

役に立ちましたか?

解決

invokeの最初の引数は、メソッドを呼び出すためにオブジェクトである必要があります:

checkEvents.invoke(g, p, gm)

あなたの方法はstaticあるので、あなたはまた、代わりにオブジェクト参照nullgを使用することができます。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top