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
java reflection constructor
add a comment |
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
java reflection constructor
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 listedtestReflection.class
in yourgetConstructor
call.
– chrylis
Nov 9 at 18:06
add a comment |
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
java reflection constructor
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
java reflection constructor
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 listedtestReflection.class
in yourgetConstructor
call.
– chrylis
Nov 9 at 18:06
add a comment |
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 listedtestReflection.class
in yourgetConstructor
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
add a comment |
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");
add a comment |
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.
add a comment |
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");
add a comment |
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");
add a comment |
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");
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");
answered Nov 9 at 17:55
ernest_k
17.6k41836
17.6k41836
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 9 at 17:53
Jacob G.
14.5k41860
14.5k41860
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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 yourgetConstructor
call.– chrylis
Nov 9 at 18:06