2013-10-21 54 views
1

我正在写一个函数从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的

+0

这是在你的代码行被触发NPE ? (堆栈跟踪中的第133行) – Gagravarr

+0

NPE:“Iterator rowIter = mySheet.rowIterator();”我检查并看到mySheet为空 –

+1

那就是你的问题了!检查工作表是否为空,并给出一个合适的错误/例外,让用户知道他们提供了一个无效的工作表名称 – Gagravarr

回答

1

你在评论中说,给NPE的路线是:

Iterator<Row> rowIter = mySheet.rowIterator(); 

你的问题是,你不检查如果你试图获取的工作表真的是前isted。如果您尝试使用无效名称获取工作表,则会返回空值。从Workbook JavaDocs

返回:表与提供或空的名称,如果它不存在

所以,你的代码应该然后是更多的东西一样:

HSSFSheet mySheet= myWorkBook.getSheet(sheetname); 
if (mySheet == null) { 
    throw new IllegalArgumentException("No sheet exists with name " + sheetName); 
} 

或者,从您的代码中返回null,然后在您的调用代码中检查它(而不是捕获异常)。

如果你不知道什么表工作簿确有(这可能不是你在奇数的情况下想),尝试:

for (int sn=0; sn < myWorkBook.getNumberOfSheets(); sn++) { 
    System.out.println("Sheet " + sn + " is called " + myWorkBook.getSheetName(sn)); 
} 
+0

但问题是:我有我的Excel文件中的名称表...所以我不知道它为什么返回空? –

+0

您的工作表可能不会被称为您当时的想法!我已经添加了一小段代码,可以让你查看你的床单真的叫什么 – Gagravarr

+0

抱歉,迟到的回复。我检查了你的代码。我找到。它调用其他工作表名称(X)而不是我所期望的。 X之前被删除....感谢您的帮助,我找到了根本原因:) –