dimanche 1 octobre 2017

NoSuchMethodException while getting the constructor through reflection in java [duplicate]

Below is the code in which I am trying to get the constructor of java class ArrayList which accepts one Integar value. But I am getting NoSuchMethodException.

Actual error is: java.lang.NoSuchMethodException: java.util.ArrayList.(java.lang.Integer)

import java.util.*;
import java.lang.reflect.Constructor;
public class MyClass {
    public static void main(String args[]) {
    try
    {
        Class obj = Class.forName("java.util.ArrayList");
        Class [] arglist = new Class[1];
        arglist[0] = Integer.class;

        Constructor con = obj.getConstructor(arglist);
        System.out.println("Constructor found:" +con.toString());
    }
    catch(ClassNotFoundException a)
    {
        System.out.println(a);

    }
    catch (NoSuchMethodException b)
    {
        System.out.println(b);
    }
}

}





Aucun commentaire:

Enregistrer un commentaire