Java: Illegal argomento Exception
-
21-09-2019 - |
Domanda
Ricevo un IllegalArgumentException
, ma non riesco a capire perché.
La funzione sto cercando di accesso:
private static Player checkEvents(Player[] players, GameMaster bananas)
Il codice problematico:
@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
}
Il fallimento:
testCheckEvents(nth.bananas.GameTest)
java.lang.IllegalArgumentException: wrong number of arguments
Che cosa sto facendo di sbagliato?
Soluzione
Il primo argomento invoke
deve essere un oggetto su cui richiamare il metodo:
checkEvents.invoke(g, p, gm)
Dal momento che il metodo è static
, è anche possibile utilizzare null
anziché l'g
oggetto di riferimento.
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow