0

Why does this fail with the error :

Args are: -normi -nosplash
Exception in thread "main" java.lang.IllegalArgumentException: wrong 
     number of arguments
    at sun.reflect.NativeConstructorAccessorImpl
    .newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl
    .newInstance(NativeConstructorAccessorImpl.java:39)
    at sun.reflect.DelegatingConstructorAccessorImpl
    .newInstance(DelegatingConstructorAccessorImpl.java:27)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    at TSStack.main(TSStack.java:14)

Here is the code:

public static void main(String args[]) throws Exception {
    System.out.println("Args are: " + args[0]+ " " + args[1] );
        try {
            Constructor<Site> c = Site.class.getDeclaredConstructor();
            c.setAccessible(true); // use reflection to get access to  
                                      //this private constructor
            c.newInstance( (Object[])args );
          } catch (InvocationTargetException x) {
            x.printStackTrace();
          } catch (NoSuchMethodException x) {
            x.printStackTrace();
          } catch (InstantiationException x) {
            x.printStackTrace();
          } catch (IllegalAccessException x) {
            x.printStackTrace();
          }     
}
1
  • 1
    Cute - you tried to catch a whole bunch of exceptions but still ended up missing this one anyway. Commented Jul 24, 2011 at 1:00

2 Answers 2

4

When you have this line:

Site.class.getDeclaredConstructor();

You are getting back a constructor for the Site class that takes in no arguments, since getDeclaredConstructor is a variadic function that takes in as arguments a list of Class<?> objects describing the argument types. Since you didn't list anything, you're getting back a nullary constructor.

However, you then try creating the object by calling

c.newInstance( (Object[])args );

This tries passing in args as arguments. Unless args is empty, this will cause a problem, since you explicitly asked for a no-argument constructor.

EDIT: Since the constructor you're looking for (based on your above comment) wants to take in a variable number of Strings as an argument, you want to look for a constructor that (I believe) takes in an array of Strings as its argument, since internally variadic functions are implemented using arrays. You could do this as follows:

Constructor<Site> c = Site.class.getDeclaredConstructor(String[].class);
c.setAccessible(true); // use reflection to get access to this private constructor
c.newInstance( (Object[])args );

More importantly though, why are you using reflection at all? It's faster, cleaner, and safer just to write

new Site(args);

This allows Java to statically verify the safety of your code.

Hope this helps!

Sign up to request clarification or add additional context in comments.

2 Comments

Cant call "new Site(args) because the only constructor is private, and it taks zero args.
@djangofan- Out of curiosity, then, why are you trying to pass an array of arguments to it?
3

Site.class.getDeclaredConstructor() will return you default constructor with no arguments, so you must pass it an empty array of arguments which is not the case in your example (otherwise you would fail on the first line with System.out.println()).

3 Comments

The Constructor in question should be capable of taking a variable number of arguments and so why wont it allow me to pass a full argument list? This is a mystery to me.
@djangofan- You should probably try to get a constructor that takes a String[] as an argument, then; see my post for how to do this.
@djangofan It does take variable number of arguments, which is 0 in your case. If you have a constructor that takes String[], that's the one you should be looking for, not empty argument one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.