1

I am trying to understand how reflection works, i cant able to figure out the problem. In the second for loop method.length is not fetching method from another program.

Following is the code:

public class DriverScript 
{

    public static ActionKeywords actionKeywords;
    public static String sActionKeyword;
    public static Method method[];

    public DriverScript() throws NoSuchMethodException, SecurityException 
    {
        actionKeywords = new ActionKeywords();
        method = actionKeywords.getClass().getMethods();
    }

    public static void main(String[] args) throws Exception 
    {
        System.out.println(method);
        String sPath = "C:\\Users\\mag6\\Desktop\\toolsqa.xlsx";
        ExcelUtils.setExcelFile(sPath, "Test Steps");

        for (int iRow = 1; iRow <= 9; iRow++) 
        {
            sActionKeyword = ExcelUtils.getCellData(iRow, 3);
            execute_Actions();
        }
    }

    private static void execute_Actions() throws Exception 
    {
        for (int i = 0; i < 11; i++) 
        {
            if (method[i].getName().equals(sActionKeyword)) 
            {
                method[i].invoke(actionKeywords);
                break;
            }
        }
    }
}
NarendraR
  • 7,577
  • 10
  • 44
  • 82
selvakumar
  • 620
  • 1
  • 8
  • 30

3 Answers3

1
public class Test2 {
        //Reflection in Keyword driven Framework
    /**
    * @authors Venkadesh,Selvakumar work name : Test
    **/
    static Class Class1 = ActionKeywords.class;
    static ActionKeywords Keywords = new ActionKeywords();
    public static void main(String[] args) throws Exception {

        String sPath = "C:\\Users\\mag6\\Desktop\\toolsqa.xlsx";
        ExcelUtils.setExcelFile(sPath, "Test Steps");
        Method[] methods = Class1.getDeclaredMethods();

        for (int i = 1; i <= 9; i++) {
            // This to get the value of column Action Keyword from the excel
            String sActionKeyword = ExcelUtils.getCellData(i, 3);
            for (int j = 1; j <= 9; j++) {
                if (methods[j].getName().equals(sActionKeyword)) {
                    try {

                        methods[j].invoke(Keywords);
                    } catch (Exception e) {
                    }

                    System.out.println(methods[j]);
                }

            }

        }
    }
}

try with this it works

selvakumar
  • 620
  • 1
  • 8
  • 30
0

You are not calling the constructor for DriverScript so the fields are not initialized.

Try changing your methods from static to non-static and fix from there.

Scary Wombat
  • 44,617
  • 6
  • 35
  • 64
  • I had called the constructor DriverScript . method = actionKeywords.getClass().getMethods(); is showing null value .. – selvakumar Aug 10 '17 at 08:28
  • well I have no idea what methods are available on the `ActionKeywords` class because you have not posted that code. I have fixed the problem that you posted, please mark as accepted – Scary Wombat Aug 10 '17 at 08:33
  • public static WebDriver driver;public static void openBrowser() my one of the method – selvakumar Aug 10 '17 at 08:50
0

Replace

public static Method method[];

with

public static Method method[]= ActionKeywords.class.getDeclaredMethods();
Michał Zaborowski
  • 3,911
  • 2
  • 19
  • 39
insaf
  • 1