我正在写一个函数从excel(.xls)读取数据,然后将其添加到HashMap中。阅读excel文件时出现空指针异常
它工作之前,但我现在得到错误。
我知道为什么会出现错误:这是因为我希望读取数据的表(Htest)是空的。
我检查我的Excel,有正确的名称'Htest'表。
另外我检查工作簿中的工作表数量。它返回工作簿的正确张数
我检查另一张纸:它的工作原理。
它仅会因我的工作在一张纸上错误...
我不知道为什么片为工作簿可用,但代码返回null? 我错过了什么? 有没有人有同样的问题?或者你能否给我一个暗示与它合作?
谢谢。
的错误是:
Method arguments: [email protected], "/enigma"
java.lang.NullPointerException
com.validant.enigma3.common.UIMap.readControls(UIMap.java:133)
com.validant.enigma3.common.UIMap.<init>(UIMap.java:32)
com.validant.enigma3.common.TestBase.beforeSuite(TestBase.java:24)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:606)
org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)
org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:564)
org.testng.internal.Invoker.invokeConfigurations(Invoker.java:213)
org.testng.internal.Invoker.invokeConfigurations(Invoker.java:138)
org.testng.SuiteRunner.privateRun(SuiteRunner.java:277)
org.testng.SuiteRunner.run(SuiteRunner.java:240)
org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
org.testng.TestNG.run(TestNG.java:1057)
org.apache.maven.surefire.testng.TestNGExecutor.run(TestNGExecutor.java:74)
org.apache.maven.surefire.testng.TestNGXmlTestSuite.execute(TestNGXmlTestSuite.java:92)
org.apache.maven.surefire.Surefire.run(Surefire.java:180)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:606)
org.apache.maven.surefire.booter.SurefireBooter.runSuitesInProcess(SurefireBooter.java:350)
org.apache.maven.surefire.booter.SurefireBooter.main(SurefireBooter.java:1021)
的代码是
private HashMap<String, MappingObj> readControls(String filepath, String sheetname)
throws IOException {
HashMap<String, MappingObj> controllist = new HashMap<String, MappingObj>();
//System.out.println("FILE PATH = " + filepath);
//System.out.println("SHEET NAME = " + sheetname);
FileInputStream file = new FileInputStream(filepath);
System.out.println(file.toString());
//Get the workbook instance for XLS file
HSSFWorkbook myWorkBook = new HSSFWorkbook(file);
//System.out.println("NUMBER of SHEET= "+myWorkBook.getNumberOfSheets());
HSSFSheet mySheet= myWorkBook.getSheet(sheetname);
if (myWorkBook.getSheet(sheetname)==null)
System.out.println("null");
Iterator<Row> rowIter = mySheet.rowIterator();
rowIter.next();
String[] arow = new String[3];
while (rowIter.hasNext()) {
HSSFRow row = (HSSFRow) rowIter.next();
Iterator<Cell> cells = row.cellIterator();
int i = 0;
// Check data from current cell, there is data on next cell or
// not?
while (cells.hasNext()) {
HSSFCell cell = (HSSFCell) cells.next();
arow[i] = cell.getStringCellValue().trim();
i++;
}
MappingObj mapObj = new MappingObj();
mapObj.setCodeName(arow[0]);
mapObj.setSelector(arow[1]);
mapObj.setPath(arow[2]);
controllist.put(arow[0], mapObj);
file.close();
}
return controllist;
}
的Excel是: 我在Excel中5张用的顺序: 登录文件上传SimpleTasks Htest Reference
所有工作表都具有相同的数据模式(有3列:控制名称,选择器,路径) 每列只有不同的数据。
纸张原因错误是Htest和它的数据是
控制名称选择路径
test1的cssSelector test2的
这是在你的代码行被触发NPE ? (堆栈跟踪中的第133行) – Gagravarr
NPE:“Iterator rowIter = mySheet.rowIterator();”我检查并看到mySheet为空 –
那就是你的问题了!检查工作表是否为空,并给出一个合适的错误/例外,让用户知道他们提供了一个无效的工作表名称 – Gagravarr