Reputation: 652
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;
}
}
}
}
Upvotes: 1
Views: 1597
Reputation: 1
Replace
public static Method method[];
with
public static Method method[]= ActionKeywords.class.getDeclaredMethods();
Upvotes: 0
Reputation: 652
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
Upvotes: 1
Reputation: 44813
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.
Upvotes: 0