[英]Java: Illegal Argument Exception
我收到了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
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.