Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

method.invoke

Status
Not open for further replies.

Wiszh

Programmer
Aug 3, 2000
134
US
Hi all,

I'm trying to get a servlet to dynamically load a bean with data that it recived in the parameters. I'm using introspection to find th methods of the bean created with Class.forName() (we do not wihs to hadr code anything). The problem is when I call method.invoke() using the created class as the one which the method is to be invoked upon I get java.lang.IllegalArgumentException: object is not an instance of declaring class.

The basic code is:

String beanName = "IntrospectTestBean";
Class bean = Class.forName(beanName);
Class[] parameterTypes = new Class[] {String.class};
while (parameterNames.hasMoreElements()){
try{
String param = (String)parameterNames.nextElement();
Method method = bean.getMethod("set" + param, parameterTypes);
method.invoke(bean, new Object[]{request.getParameter(param)});
}catch (NoSuchMethodException e){
}catch (IllegalAccessException e) { out.println(e);
}catch (IllegalArgumentException e) { out.println(e);
}catch (InvocationTargetException e) { out.println(e);
}

I did notice that if I explicity declare the class (i.e. the one refered to as bean) then it does work.

Any info?

Thanx

Wiszh
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top