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 / January 2006

Tip: Looking for answers? Try searching our database.

reflection: wrong number of arguments

Thread view: 
moop™ - 20 Jan 2006 07:29 GMT
Hi,
I use this class to call methods of other classes:
public class FunctionHandler {
    public static String execRoutine(String longName, List<String> args)
            throws Exception {
        String ret;

        int dot = longName.lastIndexOf('.');
        String className = longName.substring(0, dot);
        String funcName = longName.substring(dot + 1);

        printf("class name: " + className);
        printf("func name: " + funcName);
        Class c = Class.forName(className);

        Method routine = c.getMethod(funcName, List.class);
        printf("method: " +
routine.getGenericParameterTypes()[0].toString());
        ret = (String) routine.invoke(args);
        return ret;
    }
}

And this is the class whose method is going to be called:
public class Math {
    public static String C(List<String> args) {
        int n, r;

        n = Integer.parseInt(args.get(0));
        r = Integer.parseInt(args.get(1));

        Combination c = new Combination(n, r);
        return String.valueOf(c.getTotal());
    }
}

But I got the following exception:
java.lang.IllegalArgumentException: wrong number of arguments
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:585)
    at
com.gsta.enrus.services.el.FunctionHandler.execRoutine(FunctionHandler.java:23)

Why?
Stefan Schulz - 20 Jan 2006 09:41 GMT
>         ret = (String) routine.invoke(args);
>         return ret;

Look at the invoke method once more. Hint: Which object is the method
invoked on? None (since it is static)? Well, maybe you should tell the
JVM that.
Chris Smith - 20 Jan 2006 15:17 GMT
moop? <samhng@gmail.com> wrote:
>         ret = (String) routine.invoke(args);

> But I got the following exception:
> java.lang.IllegalArgumentException: wrong number of arguments

You should read the API documentation before using a method.  If you
did, it would have told you that the first parameter to invoke is a
reference to the object on which to invoke the method; and that it is
ignored and should be null for a static method.  You are trying to call
the method with no parameters.

Signature

www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation

moop™ - 22 Jan 2006 04:30 GMT
Thx, it might be ignored.
moop™ - 23 Jan 2006 03:06 GMT
But here comes the continued exceptions:

java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:585)
    at
com.gsta.enrus.services.el.FunctionHandler.execRoutine(FunctionHandler.java:24)
Chris Smith - 24 Jan 2006 14:57 GMT
moop? <samhng@gmail.com> wrote:
> But here comes the continued exceptions:
>
[quoted text clipped - 7 lines]
>     at
> com.gsta.enrus.services.el.FunctionHandler.execRoutine(FunctionHandler.java:24)

That's a wrapper for a different exception.  Look further down the stack
trace.

Signature

www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation



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



©2009 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.