Java: Ilegal Excepción Argumento
-
21-09-2019 - |
Pregunta
Recibo un IllegalArgumentException
, pero no puedo entender por qué.
La función Estoy intentando acceder a:
private static Player checkEvents(Player[] players, GameMaster bananas)
El código problemático:
@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
}
El fracaso:
testCheckEvents(nth.bananas.GameTest)
java.lang.IllegalArgumentException: wrong number of arguments
¿Qué estoy haciendo mal?
Solución
El primer argumento de invoke
debe ser un objeto en el que para invocar el método:
checkEvents.invoke(g, p, gm)
Debido a que su método es static
, también puede utilizar en lugar de la null
g
referencia de objeto.
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow