title: Add method to arraylist and invoke method dynamically
body: <p>I have added method to arraylist and trying to call method (ui.mList.get(0) see code sample 1) in main to execute but java does not execute any method. However, I found other sample code to execute method as give below (see code sample 2), but when I run the code system display exception </p>
  
  <h2>    code sample 1:</h2>
  
  <pre><code>class UserLogin{
  List&lt;String&gt; mList = new ArrayList();
  
  public void testAnswerCorrect(){
  //My code
  }
  
  public void listMethodsUsingReflection(){
  Class personClass = UserLogin.class;
  Method[] methods = personClass.getDeclaredMethods();
  int i=0;
          for (Method method : methods) {
              if(method.getName().toString().contains("test")){
              mList.add(i,method.getName());
              i++;
              }
          }
  </code></pre>
  
  <p>}</p>
  
  <pre><code>public static main(String arg[]){
  
      UserLogin ul = new UserLogin();
      ul.listMethodsUsingReflection();
      ul.mList.get(0);
  }
  </code></pre>
  
  <p>Running above code does not do anything.</p>
  
  <h2>Sample code 2</h2>
  
  <pre><code>            Object obj = null;
      UserLogin ul = new UserLogin();
      ul.listMethodsUsingReflection();
      String className = "com.test.script.UserLogin";
      Class c = Class.forName(className);
      obj = c.newInstance();
      Class[] argTypes = new Class[] { String[].class };
      String sMethod =ul.mList.get(0).toString();
      Method method = c.getDeclaredMethod(sMethod, argTypes);
      method.invoke(obj, null);
  </code></pre>
  
  <p>Running above code system give exception:</p>
  
  <pre><code>Exception in thread "main" java.lang.NoSuchMethodException:com.test.script.UserLogin.testAnswerCorrect([Ljava.lang.String;)
  at java.lang.Class.getDeclaredMethod(Unknown Source)
  at com.test.script.UserLogin.main(UserLogin.java:180)
  </code></pre>
  
  <p>Is something missing or there is some other way to execute method. Also, I will be adding all the method in array list which will be read from xml file and need to execute the code dynamically. Method name will be given by user at the time to writing test script/code.</p>
  
  <p>I would appreciate your inputs</p>
  
  <p>Thanks,</p>