Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / July 2007

Tip: Looking for answers? Try searching our database.

question about reflection

Thread view: 
Alexandre Jaquet - 04 Jul 2007 15:26 GMT
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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.