'Why it is giving me no such method exception?
import java.lang.reflect.Constructor;
class tr1 {
public static void main(String[] args) {
try {
if (args.length < 1)
throw(new Exception("baby its wrong"));
Class s = Class.forName(args[0]);
Constructor c = s.getDeclaredConstructor(int.class, char.class, String.class, String.class);
Object o = c.newInstance(Integer.parseInt(args[1]), args[2].charAt(0), args[3], args[4]);
System.out.println("description of object " + o);
} catch (Exception e) {
System.out.println(e);
}
}
}
class A {
public A(int a, char c, String... strings){
System.out.println(a);
System.out.println(c);
for (String q:strings) {
System.out.println(q);
}
}
}
Why this code is giving me NoSuchMethod exception? Any solution for it?
Solution 1:[1]
Because an ellipsis is syntactic sugar for an array.
You should:
s.getDeclaredConstructor(int.class, char.class, String[].class);
(and as @JonSkeet mentions in his answer, you should also make your third argument an array in the .newInstance() invocation)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | fge |
