title: How to use getConstructor with Generic class?
body: <p>This is my first brush with reflection and generics in Java so please pardon my ignorance. I am trying to instantiate a class using reflection and generics but getting error in my toy program. The goal is to instantiate with constructor of inst class.</p>
  
  <p>Code:</p>
  
  <pre><code>/*
  *This is the builder class to build an instance
  */
  package generics.expClassT;
  import java.lang.reflect.Constructor;
  import java.lang.reflect.InvocationTargetException;
  
  public class builder {
      public static&lt;E&gt; E createInst(Class&lt;E&gt; c) throws InstantiationException, IllegalAccessException {
          //Class&lt;?&gt;[] type = new Class&lt;?&gt;[1];
          //type[0] = inst.class;
          try {
              Constructor&lt;E&gt; ctor = c.getConstructor(c); //c.getConstructor(type);
              return (ctor.newInstance("Testing"));
          } catch (NoSuchMethodException | SecurityException | IllegalArgumentException | InvocationTargetException e) {
              e.printStackTrace();
              return null;
          }
      }
  }
  
  
  /*
  * This is the class for which we need an instance
  */
  package generics.expClassT;
  public class inst {
  private String var;
  public inst(String s) { 
      this.var = s;
  }
  public String getVar() {
      return var;
  }
  
  public static void main(String args[]){
      inst instObj;
      try {
          instObj = builder.createInst(inst.class);
          System.out.println(instObj.getVar());
      } catch (InstantiationException | IllegalAccessException e) {
          //e.printStackTrace();
      }
  
  }
  }
  </code></pre>
  
  <p>Exception: </p>
  
  <pre><code>java.lang.NoSuchMethodException: generics.expClassT.inst.&lt;init&gt;(generics.expClassT.inst)
  at java.lang.Class.getConstructor0(Class.java:2800)
  at java.lang.Class.getConstructor(Class.java:1708)
  at generics.expClassT.builder.createInst(builder.java:15)
  at generics.expClassT.inst.main(inst.java:19)
  Exception in thread "main" java.lang.NullPointerException
  at generics.expClassT.inst.main(inst.java:20)
  </code></pre>
  
  <p>Thank you in advance for your time and assistance!!</p>