Frage

I am writing a function to read data from excel (.xls) then add it to a HashMap.

It work before but I get the error now.

I know why the error is raised: it is because of the sheet (Htest) that I want to read data is null.

I check my excel, there is the sheet with correct name of 'Htest'.

Also I check the number of sheet in workbook. It returns the correct number of sheets that the workbook has

I check the other sheet: It works.

It only throws error for a sheet that I am working on...

I dont know why the sheet is available in workbook but the code returns null?? I missed something? Does anyone have the same problem? or can you give me a hint to work with it?

Thank you.

The error is:

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)

The code is

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

The excel is: I have 5 sheets in the excel with the order: Login FileUpload SimpleTasks Htest Reference

All sheets have the same data schema (there are 3 columns: Control name, Selector, Path) Just different datafor each column.

The sheet cause error is Htest and its data is

Control Name Selector Path

test1 cssSelector test2

War es hilfreich?

Lösung

You've said in the comments that the line giving the NPE is:

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

Your problem is that you're not checking if the sheet you tried to fetch really existed. If you try to fetch a sheet with an invalid name, you'll get back null. From the Workbook JavaDocs:

Returns: Sheet with the name provided or null if it does not exist

So, your code should then be something more like:

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

Alternately, return null from your code there, and check for that in your calling code (rather than catching the exception).

If you're not sure what Sheets your Workbook really has (which might not be what you thought in the odd case), try:

for (int sn=0; sn < myWorkBook.getNumberOfSheets(); sn++) {
    System.out.println("Sheet " + sn + " is called " + myWorkBook.getSheetName(sn));
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top