RE: Null Pointer exception in Keyword Driven Framework

hi Lakshay & Team,

Thanks for this wonderful site, Like me, you are helping lot of people out there. God bless you all.

I am getting same error, after debugging , error throws in execute_Actions() method, as you told sActionKeyword, it has a value openBrowser but when it enters the execute_Action method Method method.length will be null.

This the code

 

package executionEngine;
import java.lang.reflect.Method;

import config.ActionKeywords;

import utility.ExcelUtils;
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 {

String sPath = “..\\src\\dataEngine\\DataEngine.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<method.length;i++){

if (method[i].getName().equals(sActionKeyword)){

method[i].invoke(actionKeywords);

break;

}

}

}

}
Add Comment
5 Answers

hi Lakshay,

Thanks for giving the feedback. Hats off to your dedication.

As you told, I have rechecked, but didn’t get any help from development team, hence i re-coded myself and it is working fine now, but i need your help in suggesting the below code, i have removed the constructor etc…, please suggest me what are the disadvantages of this code, but the code is working absolutely fine as expected.

package executionEngine;
        import java.lang.reflect.Method;
        import config.ActionKeywords;
        import utility.ExcelUtils;
    public class DriverScript {
    public static String sActionKeyword;
    public static void main(String[] args) throws Exception {
        // no parameter
        Class noparams[] = {};
        String sPath = "..\\src\\dataEngine\\DataEngine.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 {
        Class cls = Class.forName("config.ActionKeywords");
        Object obj = cls.newInstance();
        Method method[] = cls.getMethods();
        for (int i = 0; i < method.length;i++){
            if (method[i].getName().equals(sActionKeyword)){
                method[i].invoke(obj, null);
            break;
            }
        }
    }
}

thanks

Default Answered on November 9, 2014.

Hi Kiran,

In java you will always find many ways to do the same trick, please choose your answer as best answer and close the question. I am happy that you are able to resolve it your own.
http://forumsqa.com/selenium-webdriver/how-to-choose-best-answer-insert-code-vote-up-an-answer/

Regards,
Lakshay Sharma

on November 10, 2014.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.