Javaの:不正な引数例外
-
21-09-2019 - |
質問
私は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
あるので、あなたはまた、代わりにオブジェクト参照null
のg
を使用することができます。
所属していません StackOverflow