java.lang.NoSuchMethodException when invoke method dynamic

hi!

i have some trouble when i invoked hibernate PO class method dymaic by parameters, here is a piece of code, can someone help me? thanks in advance! please!

String fieldname = field.getName().toLowerCase();

String methodName = "get" + fieldname.replaceFirst(fieldname.substring(0, 1),fieldname.substring(0,1).toUpperCase());

Method methodFinal;

try {

methodFinal = value.getClass().getDeclaredMethod(methodName, new Class[] {PaymentPO.class});

return methodFinal.invoke(value, null);

} catch (SecurityException e) {

e.printStackTrace();

} catch (NoSuchMethodException e) {

e.printStackTrace();

} catch (IllegalArgumentException e) {

e.printStackTrace();

} catch (IllegalAccessException e) {

e.printStackTrace();

} catch (InvocationTargetException e) {

e.printStackTrace();

}

[893 byte] By [Belial-or-Angela] at [2007-10-2 7:19:02]
# 1

I don't know what your error is, but here are a few suggestions:

* print out the method name.

* print out the methods for the class on which you're trying to invoke the method

* rather than trying to use this means of invoking a property getter, look into some of the javabeans introspection classes:

http://java.sun.com/j2se/1.5.0/docs/api/java/beans/Introspector.html

http://java.sun.com/j2se/1.5.0/docs/api/java/beans/BeanInfo.html

http://java.sun.com/j2se/1.5.0/docs/api/java/beans/PropertyDescriptor.html

jverda at 2007-7-16 20:54:35 > top of Java-index,Java Essentials,Java Programming...
# 2
thanks jverd!i can affirm the PO class have the specific method, may be the codes is wrong as above!
Belial-or-Angela at 2007-7-16 20:54:35 > top of Java-index,Java Essentials,Java Programming...
# 3
Was the method declared in that class or a parent class? I think getDeclaredMethods only gets methods defined in that class, not ones that are inherited. Double check though, as I might be wrong.
jverda at 2007-7-16 20:54:35 > top of Java-index,Java Essentials,Java Programming...
# 4

OK! this problem was solved!

the code was wrong! my class method has no parameter, so the second parameter of "getDeclaredMethod" must be fill with null,

right code as follow:

methodFinal = value.getClass().getDeclaredMethod(methodName, ull);

methodFinal.invoke(value, null);

thanks all persons attended !

Belial-or-Angela at 2007-7-16 20:54:35 > top of Java-index,Java Essentials,Java Programming...
# 5
Ah, yeah. I should've seen that. Good work sleuthing it out yourself!
jverda at 2007-7-16 20:54:35 > top of Java-index,Java Essentials,Java Programming...