question about reflection

  • Thread starter Alexandre Jaquet
  • Start date
A

Alexandre Jaquet

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
 
A

Alexandre Jaquet

Finaly solved by simply modify the method.invoke(null, args); to
method.invoke(new MyClass, args);

Cheers
 
J

Joshua Cranmer

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,968
Messages
2,570,152
Members
46,698
Latest member
LydiaHalle

Latest Threads

Top