【问题标题】:Got null pointer exception when reading excel file读取excel文件时出现空指针异常
【发布时间】:2013-10-21 07:50:41
【问题描述】:

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

代码是

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 参考

所有工作表都具有相同的数据架构(有 3 列:控件名称、选择器、路径) 只是每列的数据不同。

工作表原因错误是Htest,它的数据是

控件名称选择器路径

test1 cssSelector test2

【问题讨论】:

  • 代码中的哪一行触发了 NPE? (堆栈跟踪中的第 133 行)
  • NPE at: "Iterator rowIter = mySheet.rowIterator();"我检查并看到 mySheet 为空
  • 那是你的问题!检查工作表是否为空,并给出适当的错误/异常,让用户知道他们提供了无效的工作表名称
  • @Gagravarr 你应该将该评论转换为答案,然后它可以被投票,甚至可能被接受;-)

标签: java nullpointerexception apache-poi poi-hssf


【解决方案1】:

您在 cmets 中说过,给出 NPE 的行是:

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

您的问题是您没有检查您尝试获取的工作表是否真的存在。如果您尝试获取名称无效的工作表,您将返回 null。来自Workbook JavaDocs

返回:具有提供名称的工作表,如果不存在则为 null

所以,你的代码应该更像:

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));
}

【讨论】:

  • 但问题是:我的 excel 文件中有具有该名称的工作表...所以我不知道为什么它返回 null???
  • 你的工作表可能不是你想象的那样!我添加了一个 sn-p 代码,可让您检查您的工作表的真正名称
  • 抱歉回复晚了。我检查了你的代码。我找到。它调用其他工作表名称 (X) 而不是我所期望的。 X之前被删除了....感谢您的帮助,我找到了根本原因:)
猜你喜欢
  • 1970-01-01
  • 2012-05-13
  • 1970-01-01
  • 2019-03-26
  • 1970-01-01
  • 2015-04-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多