Null pointer exception - if(data.get("Runmode").equals("N") | Selenium Forum
M
Posted on 14/12/2016
Hi Ashish,

I am using the below code from module no 21. the code getting failed with null pointer exception in line

if(data.get("Runmode").equals("N")){

but if i remove the line, code getting executed, but the iteration is happening for 2 times, but it takes the data from the first row only.

please help me.


public class Gmaillogin {


ExtentReports rep = ExtentManager.getInstance();
String testname = "GmailTest";

ExtentTest test;
Xls_Reader xls = new Xls_Reader(Constants.SUITEA_XLS);

@Test(dataProvider="getData")
public void dologin(Hashtable<String,String> data){// throws Throwable{
test = rep.startTest(testname);
test.log(LogStatus.INFO, data.toString());

System.out.println(data.get(3)+ "TENSION");
System.out.println(data.get(runmode)+"runmode value");

if(data.get("Runmode").equals("N")){

test.log(LogStatus.SKIP, "Skipping the test as runmode as N");
throw new SkipException("Skipping the test as runmode as N");
}
test.log(LogStatus.INFO, "starting gmail login test");


Keywords app = new Keywords(test);
test.log(LogStatus.INFO, "Executing keywords");
try {
app.executeKeywords(testname, xls);
} catch (Throwable e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
test.log(LogStatus.PASS, "Test Passed");
//tomorrow
app.getGenericKeywords().reportFailure("testing error");


}
@AfterTest
public void quit(){
if(rep!=null){
rep.endTest(test);
rep.flush();
}
}


@DataProvider
public Object[][] getData(){
String sheetName="Data";
//String testCaseName="TestA";
int testStartRowNum=1;
while(!xls.getCellData(sheetName, 0, testStartRowNum).equals(testname)){
testStartRowNum++;
}
System.out.println("Test starts from row - "+ testStartRowNum);
int colStartRowNum=testStartRowNum+1;
int dataStartRowNum=testStartRowNum+2;
System.out.println("colStartRowNum - "+ colStartRowNum);
System.out.println("dataStartRowNum - "+ dataStartRowNum);
// calculate rows of data
int rows=0;
while(!xls.getCellData(sheetName, 0, dataStartRowNum+rows).equals("")){
rows++;
}
System.out.println("Total rows are - "+rows );

//calculate total cols
int cols=0;
while(!xls.getCellData(sheetName, cols, colStartRowNum).equals("")){
cols++;
}
System.out.println("Total cols are - "+cols );
Object[][] data = new Object[rows][1];
//read the data
int dataRow=0;
Hashtable<String,String> table=null;
for(int rNum=dataStartRowNum;rNum<dataStartRowNum+rows;rNum++){
table = new Hashtable<String,String>();
for(int cNum=0;cNum<cols;cNum++){
String key=xls.getCellData(sheetName,cNum,colStartRowNum);
String value= xls.getCellData(sheetName, cNum, rNum);
table.put(key, value);
// 0,0 0,1 0,2
//1,0 1,1
}
data[dataRow][0] =table;
dataRow++;
}
return data;
}
}


Output

Test starts from row - 1
colStartRowNum - 2
dataStartRowNum - 3
Total rows are - 2
Total cols are - 3
nullTENSION
nullrunmode value
nullTENSION
nullrunmode value
log4j:WARN No appenders could be found for logger (freemarker.cache).
log4j:WARN Please initialize the log4j system properly.
FAILED: dologin({Username=shyamjanpan, Password=Password, Runmode =Y})
java.lang.NullPointerException
at com.BoyleHybrid.core.hybrid.testcases.Gmaillogin.dologin(Gmaillogin.java:36)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:85)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:639)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:821)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1131)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:124)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:108)
at org.testng.TestRunner.privateRun(TestRunner.java:773)
at org.testng.TestRunner.run(TestRunner.java:623)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:357)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:352)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:310)
at org.testng.SuiteRunner.run(SuiteRunner.java:259)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1185)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1110)
at org.testng.TestNG.run(TestNG.java:1018)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:236)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:81)

FAILED: dologin({Username=fsdf, Password=fdsf, Runmode =N})
java.lang.NullPointerException
at com.BoyleHybrid.core.hybrid.testcases.Gmaillogin.dologin(Gmaillogin.java:36)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:85)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:639)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:821)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1131)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:124)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:108)
at org.testng.TestRunner.privateRun(TestRunner.java:773)
at org.testng.TestRunner.run(TestRunner.java:623)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:357)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:352)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:310)
at org.testng.SuiteRunner.run(SuiteRunner.java:259)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1185)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1110)
at org.testng.TestNG.run(TestNG.java:1018)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:236)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:81)


===============================================
Default test
Tests run: 2, Failures: 2, Skips: 0
===============================================

M
Replied on 18/12/2016

[quote:3rb18c5w]java.lang.NullPointerException[/quote:3rb18c5w]

null pointer exception comes when java is trying to read something that is not present. maybe your program is trying to read something in empty row/col in excel sheet.

try to debug the eclipse to find what exactly is eclipse trying to read.