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 Answer(s)

    Hi Kiran,

    Use a break opint at this code:
           actionKeywords = new ActionKeywords();
          method = actionKeywords.getClass().getMethods();

    See if the method value is null or not, if it is null it means some problem in actionKeywords class. Frankly speaking code looks fine to me and it is hard to investigate like this without debugging on your system. I should suggest you to take any dev help from your project, I am sure they would be able to give you the root cause.

    Regards,
    Lakshay Sharma

    Professor Answered on November 7, 2014.
    Add Comment

    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

    The reason you are getting null pointer exception in your original code is that, you are not creating the object of DriverScript class. Once you create an object the constructor will get invoked and you won’t get null pointer exception.

    Thanks

    Train Answered on March 28, 2015.
    Add Comment

    One more solution is to move
    actionKeywords = new ActionKeywords();
    method = actionKeywords.getClass().getMethods();
    into execute_Actions() method.
    It will solve the problem also then there is no need of DriverScript() method.

    Default Answered on May 6, 2015.
    Add Comment

    The NullReferenceException is designed as a valid runtime condition that can be thrown and caught in normal program flow. It indicates that you are trying to access member fields, or function types, on an object reference that points to null. That means the reference to an Object which is not initialized.

    Johnson

    Default Answered on October 5, 2016.
    Add Comment
  • Your Answer

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