0

This code tries to iterate over an excel file and load the data to a List<List<String>> but it throws java.lang.NullPointerException at resultList.add(rrow) but it is not clear what is the problem with it:

    import org.apache.poi.ss.usermodel.Cell;
    import org.apache.poi.ss.usermodel.Row;
    import org.apache.poi.ss.usermodel.Sheet;
    import org.apache.poi.ss.usermodel.Workbook;
    import org.apache.poi.xssf.usermodel.XSSFWorkbook;
    
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.Iterator;
    import java.util.List;
    
    public class ReadExcel {
        public List<List<String>> resultList;
    
        public List<List<String>> ReadExcelToList(String csvPath) throws IOException {
            try {
    
                FileInputStream excelFile = new FileInputStream(csvPath);
                Workbook workbook = new XSSFWorkbook(excelFile);
                System.out.println(workbook.getSheetName(0));
                Sheet datatypeSheet = workbook.getSheetAt(0);
                Iterator<Row> iterator = datatypeSheet.iterator();
                while (iterator.hasNext()) {
    
                    Row currentRow = iterator.next();
                    Iterator<Cell> cellIterator = currentRow.iterator();
                    List<String> rrow = new ArrayList<>();
                    while (cellIterator.hasNext()) {
    
                        Cell currentCell = cellIterator.next();
                        switch (currentCell.getCellType()) {
                            case Cell.CELL_TYPE_STRING:
                                rrow.add(currentCell.getStringCellValue());
                                break;
                            case Cell.CELL_TYPE_NUMERIC:
                           rrow.add(String.valueOf(currentCell.getNumericCellValue()));
                             break;
                        }
                    }
                    resultList.add(rrow);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return resultList;
        }
    }

java.lang.NullPointerException
    at ReadExcel.readExcelToList(ReadExcel.java:40)
    at BasicCSVReader.main(BasicCSVReader.java:35)
James Westgate
  • 11,306
  • 8
  • 61
  • 68
Mohammad
  • 41
  • 5
  • 1
    You have located the line of code that is throwing `NullPointerException`. That means something on that line is null and it obviously isn't `rrow`, so what must it be? Now look through your code for all occurrences of `resultList` and see if it is ever assigned a value. That's right, it never is. Note that since JDK 14, the error message of `NullPointerException` indicates the thing that is null. Refer to [JEP 358](https://openjdk.java.net/jeps/358). – Abra Jan 29 '22 at 15:10
  • Does this answer your question? [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Sam Jan 29 '22 at 15:46

1 Answers1

2

Your resultList was never created (it's null). You can fix it by defining it as follows:

public List<List<String>> resultList = new ArrayList<>();
gawi
  • 2,843
  • 4
  • 29
  • 44