I have written a java code using JXL api to read data from excel & return the table in form of 2D array. I'm getting a null pointer exception while using getRow() My Code is as follows,
public String[][] getExcelData(String xlPath, String shtName)throws Exception
{
int ci,cj;
Workbook workbk = Workbook.getWorkbook(new File(xlPath));
Sheet sht = workbk.getSheet(0);
Cell Start = sht.findCell("Start");
System.out.println("Start cell found");
Cell End = sht.findCell("End");
int startRow = Start.getRow();
System.out.println("start row number"+startRow);
int endRow = End.getRow();
int startColumn = Start.getColumn();
int endColumn = End.getColumn();
/*rowCount = sht.getRows();
colCount = sht.getColumns();*/
int tableStartRow = startRow+1;
int tableStartColumn = startColumn+1;
int tableEndRow = endRow-1;
int tableEndColumn = endColumn-1;
//tabArray = new String[rowCount-1][colCount];
tabArray = new String[tableEndRow-1][tableEndColumn-1];
System.out.println("#row: " + rowCount);
System.out.println("#col: " + colCount);
ci=0;
for (int i=1;i<=tableEndRow-1;i++, ci++)
{
cj=0;
for (int j=1;j<=tableEndColumn-1;j++, cj++)
{
System.out.println("Ci ->"+ci+" & Cj ->"+cj);
System.out.println("i ->"+i+" & j ->"+j);
System.out.println("contents"+sht.getCell(j, i).getContents());
tabArray[ci][cj]= sht.getCell(j, i).getContents();
System.out.println("Value at"+ci+""+cj+"is ->"+tabArray[ci][cj]+"\n");
}
}
return (tabArray);
}
Start cell found
SKIPPED: Test
java.lang.RuntimeException: java.lang.NullPointerException
at org.testng.internal.MethodInvocationHelper.invokeDataProvider MethodInvocationHelper.java:161)
at org.testng.internal.Parameters.handleParameters(Parameters.java:429)
at org.testng.internal.Invoker.handleParameters(Invoker.java:1383)
at org.testng.internal.Invoker.createParameters(Invoker.java:1075)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1180)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
at org.testng.TestRunner.privateRun(TestRunner.java:767)
at org.testng.TestRunner.run(TestRunner.java:617)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
at org.testng.SuiteRunner.run(SuiteRunner.java:240)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
at org.testng.TestNG.run(TestNG.java:1057)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)
Caused by: java.lang.NullPointerException
at DataProvider.DataSource.getExcelData(DataSource.java:39)
at DataProvider.DataSource.getLoginData(DataSource.java:84)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)
at org.testng.internal.MethodInvocationHelper.invokeDataProvider (MethodInvocationHelper.java:135)
... 20 more
The excel to be read is -
Any help will be appreciated !!
Your problem is spaces in your mastersheet in cell where "Start" is written. Remove extra space after/before Start. It will work.