1

Excel (.xls) からデータを読み取り、それを HashMap に追加する関数を作成しています。

以前は機能していましたが、今はエラーが発生します。

エラーが発生する理由はわかっています。データを読み取ろうとしているシート (Htest) が null であるためです。

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 枚のシートがあります: Login FileUpload SimpleTasks Htest Reference

すべてのシートのデータ スキーマは同じです (3 つの列があります: コントロール名、セレクター、パス)。各列のデータが異なるだけです。

シート原因エラーはHtestで、そのデータは

コントロール名セレクターのパス

test1 cssSelector test2

4

1 に答える 1