简体   繁体   中英

Print Simple values Using Excel, ArrayList and Java from different columns Doesn't work If there is no match between Number rows that has values

What I am trying to do is simply print multiple values using for loop and arraylist from multiple columns with the following code

// =========The SpreadSheet=========

        File src = new File("J:\\Excel files\\mutiselect.xlsx");

        // Load file
        FileInputStream fis = new FileInputStream(src);

        // Load WB
        XSSFWorkbook wb = new XSSFWorkbook(fis);

        // Load Sheet
        XSSFSheet sh1 = wb.getSheetAt(0);


        List<String> values = new ArrayList<String>();  //values from excel will be stored within this array

        Iterator<Row> rows = sh1.rowIterator();


        while (rows.hasNext()) 
        {

            Row row = rows.next();

            if (row.getRowNum() > 0) 
            {

                values.add(row.getCell(1).getStringCellValue());

            }
        }

        System.out.println(values);


        Thread.sleep(2000);


        List<String> values2 = new ArrayList<String>();  //values from excel will be stored within this array

        Iterator<Row> rows2 = sh1.rowIterator();


        while (rows2.hasNext()) 
        {

            Row row2 = rows2.next();

            if (row2.getRowNum() > 0) 
            {

                values2.add(row2.getCell(2).getStringCellValue());

            }
        }

        System.out.println(values2);

        fis.close();
        driver.quit();

Everything works fine it there is matching number of records within excel on Column B and Column C.

But the problem occurs and it doesn't print from one of the columns or from both columns when there is no match between the number of rows that has values means if there is more value on column B than Column C, or More value on Column C than B it will not print.

Please refer to this image for illustration:

http://i.imgur.com/DO8anzu.png

If the excel is like this the values from one of the columns doesnt get printed or from both columns nothing get printed

and gives me error like this

Exception in thread "main" java.lang.NullPointerException
    at one.MultiSelectWithForLoop.main(MultiSelectWithForLoop.java:57)

Line#57 refers to this code

values.add(row.getCell(1).getStringCellValue());

or gives me this error

Exception in thread "main" java.lang.NullPointerException
    at one.MultiSelectWithForLoop.main(MultiSelectWithForLoop.java:99)

line#99 refers to this code

values2.add(row2.getCell(2).getStringCellValue());

What I want to do is print out all the values from each column individually even even if there is no match in number of values or its less or more in one column than the other to do this where do I change in the code please?

PS this code is being used for Selenium Webdriver with Java

What about checking for null before adding to the values and values2 lists?

if(row.getCell(1) != null) {
    values.add(row.getCell(1).getStringCellValue());
}

and

if(row2.getCell(2) != null) {
    values.add(values2.getCell(2).getStringCellValue());
}

From getCell() documentation

If you ask for a cell that is not defined....you get a null.

When the number of values is different in each column the Iterator still has next line so rows.hasNext() is true, but the cell is empty so row.getCell(1) is null .

When you try to get getStringCellValue() from the null cell you get NullPointerException .

Change

if (row.getRowNum() > 0)

To

if (row.getRowNum() > 0 && row.getCell(1) != null) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM