Hi,
I'm trying to write a generic method for the setter of my fields and I
doesn't know how to invoke the method
I've the following code
public void notifyPropertyChange(String name, String value) {
Class c = this.getClass();
String methodName = name.substring(0,1).toUpperCase() +
name.substring(1);
try {
Method method = c.getMethod("set" + methodName, new Class []
{String.class});
String [] args = new String[]{value};
try {
method.invoke(null, args);
}catch (IllegalAccessException ex) {
Logger.error("Error while invoking method : " + ex);
}catch (InvocationTargetException ex) {
Logger.error("Error while invoking method : " + ex);
}
}catch (NoSuchMethodException ex) {
Logger.error("Error while invoking method : " + ex);
}
but the method.invoke doesn't work.
Any idea on how to fix it ?
Thanks
Alexandre
Alexandre Jaquet - 04 Jul 2007 15:30 GMT
Finaly solved by simply modify the method.invoke(null, args); to
method.invoke(new MyClass, args);
Cheers
Joshua Cranmer - 04 Jul 2007 22:14 GMT
> Finaly solved by simply modify the method.invoke(null, args); to
> method.invoke(new MyClass, args);
>
> Cheers
Considering that the method is an instance method, method.invoke(this,
args) works better.