Getting Exception in thread “main” java.lang.NullPointerException

Hello ,

I am getting null point exception when I tried to add Java reflection  code in Driver script.


Exception in thread "main" java.lang.NullPointerException

at automationFramework.storeDriver.execute_Actions(storeDriver.java:43)

at automationFramework.storeDriver.main(storeDriver.java:37)

Code is below:


package automationFramework;

//Reflection class
import java.lang.reflect.Method;
import config.ActionKeywords;

import utility.Excelutils;
public class storeDriver {

//This is a class object, declared as 'public static'

//So that it can be used outside the scope of main[] method

public static ActionKeywords actionKeywords;

public static String sActionKeyword;

//This is reflection class object, declared as 'public static'

//So that it can be used outside the scope of main[] method

public static Method method[];
public storeDriver() throws NoSuchMethodException, SecurityException{

actionKeywords = new ActionKeywords();
method = actionKeywords.getClass().getMethods();

}
public static void main(String[] args) throws Exception {
String Excelpath = "C://Users//i329919//workspace//OnlineStore//src//dataEngine//DataEngine.xlsx";
Excelutils.setExcelFile(Excelpath, "TestSteps");
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++){

//This is now comparing the method name with the ActionKeyword value got from excel

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

//In case of match found, it will execute the matched method

method[i].invoke(actionKeywords);

//Once any method is executed, this break statement will take the flow outside of for loop

break;

}

}

}

}

Default Asked on October 21, 2014 in Selenium WebDriver.
Add Comment
  • 4 Answer(s)

    Professor Answered on October 25, 2014.
    Add Comment

    hi Lakshay,

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

    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;
    }
    }
    }
    }

    Default Answered on November 6, 2014.
    Add Comment

    I’m getting the same error. Is there any suggestion to solve that problem?

    Default Answered on May 29, 2015.
    Add Comment

    I’m also getting the same error. can anyone help..

     

    Exception in thread “main” java.lang.NullPointerException
    at executionEngine.DriverScript.execute_Actions(DriverScript.java:54)
    at executionEngine.DriverScript.main(DriverScript.java:43)

    Default Answered on June 14, 2018.
    Add Comment
  • Your Answer

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