簡體   English   中英

讀取Excel文件時出現空指針異常

[英]Got null pointer exception when reading excel file

我正在編寫一個從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張紙,其順序為:登錄FileUpload SimpleTasks Htest參考

所有工作表都具有相同的數據模式(共有3列:控件名稱,選擇器,路徑)每列只有不同的數據。

工作表原因錯誤為Htest,其數據為

控件名稱選擇器路徑

test1 cssSelector test2

您在評論中說過,提供NPE的代碼行是:

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

您的問題是您沒有檢查嘗試獲取的工作表是否確實存在。 如果您嘗試獲取名稱無效的工作表,則會返回null。 工作簿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));
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM