我使用JXL api编写了一个java代码,用于从excel& amp;中读取数据。以2D数组的形式返回表格。我在使用getRow()时遇到空指针异常 我的代码如下,
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
要阅读的擅长是 -
任何帮助将不胜感激!!
答案 0 :(得分:0)
您的问题是单元格中主题表中的空格,其中&#34;开始&#34;是写的。在开始之后/之前删除多余的空间。 它会起作用。