RE: 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;

}

}

}

}

Anila Default Asked on October 21, 2014 in Selenium WebDriver.
Add Comment
4 Answers

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

Your Answer

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