hi ,
I found that there is a error in the coding given in the web page.
things to be corrected.
public int add(int a, int b)
{
return a + b;
}
change to
public int add(int a, int b, int c)
{
return a + b + c;
}
Class partypes[] = new Class[2];
change the above to this
Class partypes[] = new Class[3];
Add:
partypes[2] = Integer.TYPE;
//Please see the following:
import java.lang.reflect.*;
public class InvokeMethod {
public int add(int a, int b)
{
return a + b;
}
public static void main(String args[]){
try {
Class cla = Class.forName("InvokeMethod");
Class partypes[] = new Class[2];
partypes[0] = Integer.TYPE;
partypes[1] = Integer.TYPE;
Method method = cla.getMethod(
"add", partypes);
InvokeMethod invoke = new InvokeMethod();
Object arg[] = new Object[2];
arg[0] = new Integer(30);
arg[1] = new Integer(40);
//arg[2] = new Integer(50);
Object object = method.invoke(invoke, arg);
Integer integer = (Integer)object;
System.out.println(integer.intValue());
}
catch (Throwable e) {
System.out.println(e);
}
}
}
wrong number of argumentsmaurysnake October 13, 2011 at 7:23 PM
java.lang.IllegalArgumentException: wrong number of arguments the object "arg[]" contains 3 elements and the method add only accept 2 arguments.
code given in download and web page is differentdeepika August 28, 2012 at 3:52 PM
hi , I found that there is a error in the coding given in the web page. things to be corrected. public int add(int a, int b) { return a + b; } change to public int add(int a, int b, int c) { return a + b + c; } Class partypes[] = new Class[2]; change the above to this Class partypes[] = new Class[3]; Add: partypes[2] = Integer.TYPE;
It's Wrong CodeAsk You August 30, 2012 at 12:11 AM
//Please see the following: import java.lang.reflect.*; public class InvokeMethod { public int add(int a, int b) { return a + b; } public static void main(String args[]){ try { Class cla = Class.forName("InvokeMethod"); Class partypes[] = new Class[2]; partypes[0] = Integer.TYPE; partypes[1] = Integer.TYPE; Method method = cla.getMethod( "add", partypes); InvokeMethod invoke = new InvokeMethod(); Object arg[] = new Object[2]; arg[0] = new Integer(30); arg[1] = new Integer(40); //arg[2] = new Integer(50); Object object = method.invoke(invoke, arg); Integer integer = (Integer)object; System.out.println(integer.intValue()); } catch (Throwable e) { System.out.println(e); } } }
Post your Comment