Java 读取excel文件时出现空指针异常
我正在编写一个函数,从excel(.xls)中读取数据,然后将其添加到HashMap中 它以前工作,但我现在得到的错误 我知道产生错误的原因:正是由于工作表(Htest)的缘故,我想要读取的数据为空 我检查我的excel,有一张正确名称为“Htest”的表格 我还检查工作簿中的工作表编号。它返回工作簿的正确工作表数 我检查另一张表:它有效 它只会为我正在处理的工作表抛出错误 我不知道为什么工作簿中有工作表,但代码返回null?? 我错过了什么? 有人有同样的问题吗?或者你能给我一个提示来使用它吗 多谢各位 错误是:Java 读取excel文件时出现空指针异常,java,nullpointerexception,apache-poi,poi-hssf,Java,Nullpointerexception,Apache Poi,Poi Hssf,我正在编写一个函数,从excel(.xls)中读取数据,然后将其添加到HashMap中 它以前工作,但我现在得到的错误 我知道产生错误的原因:正是由于工作表(Htest)的缘故,我想要读取的数据为空 我检查我的excel,有一张正确名称为“Htest”的表格 我还检查工作簿中的工作表编号。它返回工作簿的正确工作表数 我检查另一张表:它有效 它只会为我正在处理的工作表抛出错误 我不知道为什么工作簿中有工作表,但代码返回null?? 我错过了什么? 有人有同样的问题吗?或者你能给我一个提示来使用它吗
Method arguments: org.testng.TestRunner@2f6d9d1c, "/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)
方法参数:org.testng。TestRunner@2f6d9d1c,“/谜”
java.lang.NullPointerException
com.validant.enigma3.common.UIMap.readControls(UIMap.java:133)
com.validant.enigma3.common.UIMap.(UIMap.java:32)
com.validant.enigma3.common.TestBase.beforeSuite(TestBase.java:24)
sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)
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.runSuitesLocal(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(本机方法)
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;
}
private HashMap readControls(字符串文件路径、字符串名称)
抛出IOException{
HashMap controllist=newhashmap();
//System.out.println(“文件路径=“+filepath”);
//System.out.println(“图纸名称=”+图纸名称);
FileInputStream文件=新的FileInputStream(filepath);
System.out.println(file.toString());
//获取XLS文件的工作簿实例
HSSFWorkbook myWorkBook=新的HSSFWorkbook(文件);
//System.out.println(“工作表编号=“+myWorkBook.getNumberOfSheets());
HSSFSheet mySheet=myWorkBook.getSheet(sheetname);
如果(myWorkBook.getSheet(sheetname)==null)
System.out.println(“空”);
迭代器rowIter=mySheet.rowitter();
rowIter.next();
字符串[]arow=新字符串[3];
while(rowIter.hasNext()){
HSSFRow row=(HSSFRow)rowIter.next();
迭代器单元格=行。单元格迭代器();
int i=0;
//检查当前单元格中的数据,下一个单元格或
//不是吗?
while(cells.hasNext()){
HSSFCell单元格=(HSSFCell)单元格。下一步();
arow[i]=cell.getStringCellValue().trim();
i++;
}
MappingObj mapObj=新的MappingObj();
mapObj.setCodeName(arow[0]);
mapObj.setSelector(arow[1]);
mapObj.setPath(arow[2]);
controllist.put(arow[0],mapObj);
file.close();
}
返回控制器;
}
excel是:
我在excel中有5张表格,顺序如下:
登录文件上载SimpleTasks Htest参考
所有图纸都具有相同的数据架构(共有3列:控件名称、选择器、路径)
只是每列的数据不同
导致错误的工作表为Htest,其数据为
控件名称选择器路径
test1 CSS选择器test2您在评论中说过,给出NPE的行是:
Iterator<Row> rowIter = mySheet.rowIterator();
或者,从那里的代码返回null,并在调用代码中检查它(而不是捕获异常)
如果您不确定工作簿中真正有哪些工作表(在这种情况下可能不是您所想的),请尝试:
for(int sn=0;sn
代码中哪一行触发了NPE?(stacktrace中的第133行)NPE在:“Iterator rowIter=mySheet.rowIterator();”我检查了一下,发现mySheet为空,这就是你的问题!检查工作表是否为空,并给出适当的错误/异常,让用户知道他们提供了无效的工作表name@Gagravarr您应该将该评论转换为答案,然后就可以进行投票,甚至可能被接受;-)但问题是:我的excel文件中有一个同名的工作表…所以我不知道为什么它返回空值???你的工作表可能不是你想象的那样!我添加了一段代码,让您可以检查工作表的真正名称,以防响应过迟。I c
HSSFSheet mySheet= myWorkBook.getSheet(sheetname);
if (mySheet == null) {
throw new IllegalArgumentException("No sheet exists with name " + sheetName);
}
for (int sn=0; sn < myWorkBook.getNumberOfSheets(); sn++) {
System.out.println("Sheet " + sn + " is called " + myWorkBook.getSheetName(sn));
}