2009-12-27 12 views
1

IllegalArgumentException이 표시되지만 그 이유를 알 수 없습니다.Java : 잘못된 인수 예외

내가 접근에 노력하고있어 기능 :

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 

내가 잘못 뭐하는 거지? 당신의 방법은 static 때문에

checkEvents.invoke(g, p, gm) 

, 당신은 또한 대신 객체 참조 gnull를 사용할 수 있습니다

답변

4

invoke에 첫 번째 인수는 메소드를 호출 할 수의 개체 수 있어야합니다.

+3

@ hjhill의 정답을 확장하려면 Method 객체가 정적 메서드를 참조하면 invoke()의 첫 번째 인수는 무시됩니다. –