java reflection constructor.newInstance gives “wrong number of arguments”









up vote
-2
down vote

favorite












How to fix my code below?



package mypackage;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;

public class testReflection
public class You
public You(String s)


public void f(String s, int i)
System.out.println(i + 100);



public static void main(String args) throws NoSuchMethodException
Constructor constructor =
You.class.getConstructor(testReflection.class, String.class);
try
You y = (You)constructor.newInstance("xzy");//Exception!!!!
System.out.println("ok");
y.f("xyz",2);
catch(Exception e)
e.printStackTrace();





The exception message is:



java.lang.IllegalArgumentException: wrong number of arguments
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at mypackage.testReflection.main









share|improve this question



















  • 1




    There was already an answer to your very recent question on how to get inner class constructor and create a new instance with it: stackoverflow.com/a/53229272/812787
    – tsolakp
    Nov 9 at 17:57










  • You specifically listed testReflection.class in your getConstructor call.
    – chrylis
    Nov 9 at 18:06














up vote
-2
down vote

favorite












How to fix my code below?



package mypackage;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;

public class testReflection
public class You
public You(String s)


public void f(String s, int i)
System.out.println(i + 100);



public static void main(String args) throws NoSuchMethodException
Constructor constructor =
You.class.getConstructor(testReflection.class, String.class);
try
You y = (You)constructor.newInstance("xzy");//Exception!!!!
System.out.println("ok");
y.f("xyz",2);
catch(Exception e)
e.printStackTrace();





The exception message is:



java.lang.IllegalArgumentException: wrong number of arguments
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at mypackage.testReflection.main









share|improve this question



















  • 1




    There was already an answer to your very recent question on how to get inner class constructor and create a new instance with it: stackoverflow.com/a/53229272/812787
    – tsolakp
    Nov 9 at 17:57










  • You specifically listed testReflection.class in your getConstructor call.
    – chrylis
    Nov 9 at 18:06












up vote
-2
down vote

favorite









up vote
-2
down vote

favorite











How to fix my code below?



package mypackage;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;

public class testReflection
public class You
public You(String s)


public void f(String s, int i)
System.out.println(i + 100);



public static void main(String args) throws NoSuchMethodException
Constructor constructor =
You.class.getConstructor(testReflection.class, String.class);
try
You y = (You)constructor.newInstance("xzy");//Exception!!!!
System.out.println("ok");
y.f("xyz",2);
catch(Exception e)
e.printStackTrace();





The exception message is:



java.lang.IllegalArgumentException: wrong number of arguments
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at mypackage.testReflection.main









share|improve this question















How to fix my code below?



package mypackage;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;

public class testReflection
public class You
public You(String s)


public void f(String s, int i)
System.out.println(i + 100);



public static void main(String args) throws NoSuchMethodException
Constructor constructor =
You.class.getConstructor(testReflection.class, String.class);
try
You y = (You)constructor.newInstance("xzy");//Exception!!!!
System.out.println("ok");
y.f("xyz",2);
catch(Exception e)
e.printStackTrace();





The exception message is:



java.lang.IllegalArgumentException: wrong number of arguments
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at mypackage.testReflection.main






java reflection constructor






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 9 at 18:06









Mark Rotteveel

58.1k1475117




58.1k1475117










asked Nov 9 at 17:50









Troskyvs

2,121924




2,121924







  • 1




    There was already an answer to your very recent question on how to get inner class constructor and create a new instance with it: stackoverflow.com/a/53229272/812787
    – tsolakp
    Nov 9 at 17:57










  • You specifically listed testReflection.class in your getConstructor call.
    – chrylis
    Nov 9 at 18:06












  • 1




    There was already an answer to your very recent question on how to get inner class constructor and create a new instance with it: stackoverflow.com/a/53229272/812787
    – tsolakp
    Nov 9 at 17:57










  • You specifically listed testReflection.class in your getConstructor call.
    – chrylis
    Nov 9 at 18:06







1




1




There was already an answer to your very recent question on how to get inner class constructor and create a new instance with it: stackoverflow.com/a/53229272/812787
– tsolakp
Nov 9 at 17:57




There was already an answer to your very recent question on how to get inner class constructor and create a new instance with it: stackoverflow.com/a/53229272/812787
– tsolakp
Nov 9 at 17:57












You specifically listed testReflection.class in your getConstructor call.
– chrylis
Nov 9 at 18:06




You specifically listed testReflection.class in your getConstructor call.
– chrylis
Nov 9 at 18:06












2 Answers
2






active

oldest

votes

















up vote
2
down vote



accepted










The hint is on this line (the constructor takes 2 parameters):



Constructor constructor =
You.class.getConstructor(testReflection.class, String.class);


You need to send an instance of testReflection to newInstance():



testReflection outerObject = new testReflection();
You y = (You)constructor.newInstance(outerObject, "xzy");





share|improve this answer



























    up vote
    5
    down vote













    From the documentation of Constructor#newInstance:




    If the constructor's declaring class is an inner class in a non-static context, the first argument to the constructor needs to be the enclosing instance; see section 15.9.3 of The Java™ Language Specification.




    Because You is an inner class, you need an instance of its enclosing class, testReflection, to create an instance of You. To do this, you can use the following:



    You y = (You) constructor.newInstance(new testReflection(), "xzy");


    I also recommend changing your class name to TestReflection as that follows the proper naming conventions.






    share|improve this answer




















      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













       

      draft saved


      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53230920%2fjava-reflection-constructor-newinstance-gives-wrong-number-of-arguments%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      2
      down vote



      accepted










      The hint is on this line (the constructor takes 2 parameters):



      Constructor constructor =
      You.class.getConstructor(testReflection.class, String.class);


      You need to send an instance of testReflection to newInstance():



      testReflection outerObject = new testReflection();
      You y = (You)constructor.newInstance(outerObject, "xzy");





      share|improve this answer
























        up vote
        2
        down vote



        accepted










        The hint is on this line (the constructor takes 2 parameters):



        Constructor constructor =
        You.class.getConstructor(testReflection.class, String.class);


        You need to send an instance of testReflection to newInstance():



        testReflection outerObject = new testReflection();
        You y = (You)constructor.newInstance(outerObject, "xzy");





        share|improve this answer






















          up vote
          2
          down vote



          accepted







          up vote
          2
          down vote



          accepted






          The hint is on this line (the constructor takes 2 parameters):



          Constructor constructor =
          You.class.getConstructor(testReflection.class, String.class);


          You need to send an instance of testReflection to newInstance():



          testReflection outerObject = new testReflection();
          You y = (You)constructor.newInstance(outerObject, "xzy");





          share|improve this answer












          The hint is on this line (the constructor takes 2 parameters):



          Constructor constructor =
          You.class.getConstructor(testReflection.class, String.class);


          You need to send an instance of testReflection to newInstance():



          testReflection outerObject = new testReflection();
          You y = (You)constructor.newInstance(outerObject, "xzy");






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 9 at 17:55









          ernest_k

          17.6k41836




          17.6k41836






















              up vote
              5
              down vote













              From the documentation of Constructor#newInstance:




              If the constructor's declaring class is an inner class in a non-static context, the first argument to the constructor needs to be the enclosing instance; see section 15.9.3 of The Java™ Language Specification.




              Because You is an inner class, you need an instance of its enclosing class, testReflection, to create an instance of You. To do this, you can use the following:



              You y = (You) constructor.newInstance(new testReflection(), "xzy");


              I also recommend changing your class name to TestReflection as that follows the proper naming conventions.






              share|improve this answer
























                up vote
                5
                down vote













                From the documentation of Constructor#newInstance:




                If the constructor's declaring class is an inner class in a non-static context, the first argument to the constructor needs to be the enclosing instance; see section 15.9.3 of The Java™ Language Specification.




                Because You is an inner class, you need an instance of its enclosing class, testReflection, to create an instance of You. To do this, you can use the following:



                You y = (You) constructor.newInstance(new testReflection(), "xzy");


                I also recommend changing your class name to TestReflection as that follows the proper naming conventions.






                share|improve this answer






















                  up vote
                  5
                  down vote










                  up vote
                  5
                  down vote









                  From the documentation of Constructor#newInstance:




                  If the constructor's declaring class is an inner class in a non-static context, the first argument to the constructor needs to be the enclosing instance; see section 15.9.3 of The Java™ Language Specification.




                  Because You is an inner class, you need an instance of its enclosing class, testReflection, to create an instance of You. To do this, you can use the following:



                  You y = (You) constructor.newInstance(new testReflection(), "xzy");


                  I also recommend changing your class name to TestReflection as that follows the proper naming conventions.






                  share|improve this answer












                  From the documentation of Constructor#newInstance:




                  If the constructor's declaring class is an inner class in a non-static context, the first argument to the constructor needs to be the enclosing instance; see section 15.9.3 of The Java™ Language Specification.




                  Because You is an inner class, you need an instance of its enclosing class, testReflection, to create an instance of You. To do this, you can use the following:



                  You y = (You) constructor.newInstance(new testReflection(), "xzy");


                  I also recommend changing your class name to TestReflection as that follows the proper naming conventions.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 9 at 17:53









                  Jacob G.

                  14.5k41860




                  14.5k41860



























                       

                      draft saved


                      draft discarded















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53230920%2fjava-reflection-constructor-newinstance-gives-wrong-number-of-arguments%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Use pre created SQLite database for Android project in kotlin

                      Darth Vader #20

                      Ondo