Twitter iPhone pliant OnePlus 11 PS5 Disney+ Orange Livebox Windows 11

Pb invoke de methode ayant comme parametre d'entre un tableau d'entier

2 réponses
Avatar
phil89
Bonsoir

J'ai eune erreur lorsque je souhaite invoker une methode ayant un
parametre .
Ce parametre etant un tableau d'entier .
Merci d'avance

Philippe


public void setArgsTab(int[]x1) {
System.out.println("Un tableau d'entiers setargsTab
"+x1.length);
}

// CAS ENTIERS TABLEAU

Object[] params2b=new Object[] { new Integer[]
{ new Integer(1),new

try
{
Class cl = class1.getClass();
Class[] mainsParamTypes = new Class[] { int
[].class };

Method mainMethod = cl.getDeclaredMethod
( "setArgsTab", mainsParamTypes );
mainMethod.invoke( class1, params2b );
}
/*catch( ClassNotFoundException e )
{
System.out.println( "Cannot find ");
}*/
catch( NoSuchMethodException e )
{
System.out.println( "Cannot find main in ");
}
catch( IllegalAccessException e )
{
System.out.println( "Cannot invoke main???
in ");
}
catch( InvocationTargetException e )
{
System.out.println( "main threw an
exception" );
e.getTargetException( ).printStackTrace( );
}

2 réponses

Avatar
Emmanuel Bourg
La méthode prend un tableau d'entiers et non un tableau d'Integers,
essaye avec :

Object[] params2b=new Object[] { new int[] { 1 } };



phil89 a écrit :
Bonsoir

J'ai eune erreur lorsque je souhaite invoker une methode ayant un
parametre .
Ce parametre etant un tableau d'entier .
Merci d'avance

Philippe


public void setArgsTab(int[]x1) {
System.out.println("Un tableau d'entiers setargsTab
"+x1.length);
}

// CAS ENTIERS TABLEAU

Object[] params2b=new Object[] { new Integer[]
{ new Integer(1),new

try
{
Class cl = class1.getClass();
Class[] mainsParamTypes = new Class[] { int
[].class };

Method mainMethod = cl.getDeclaredMethod
( "setArgsTab", mainsParamTypes );
mainMethod.invoke( class1, params2b );
}
/*catch( ClassNotFoundException e )
{
System.out.println( "Cannot find ");
}*/
catch( NoSuchMethodException e )
{
System.out.println( "Cannot find main in ");
}
catch( IllegalAccessException e )
{
System.out.println( "Cannot invoke main???
in ");
}
catch( InvocationTargetException e )
{
System.out.println( "main threw an
exception" );
e.getTargetException( ).printStackTrace( );
}



Avatar
phil89
Merci cela fonctionne