0

I am trying to use the XSSFWorkbook from org.apache.poi.ss.usermodel to read an Excel file. For some reason I am getting a Null Pointer exception when I try to pull data from one of the Sheet methods. I am very new to Java programming, and it may be something very fundamental.

import org.apache.poi.ss.usermodel.*;
public class ReadFile {

public static void main(String[] args) throws InterruptedException, IOException {

    File f = new File("C:/Users/munish/Documents/Training/Selenium/sample.xlsx");
    FileInputStream file = new FileInputStream(f);
    XSSFWorkbook workbook = new XSSFWorkbook(file);
    Sheet sheet = workbook.getSheetAt(0);

    int rowsCount = sheet.getPhysicalNumberOfRows();
    System.out.println("Total number of rows: " + (rowsCount + 1));
    for (int i = 0; i <= rowsCount; i++) {
        Row row = sheet.getRow(i);
        int colCounts = row.getPhysicalNumberOfCells();
        System.out.println("Total number of Cols: " + colCounts);
        for (int j = 0; j < colCounts; j++) {
            Cell cell = row.getCell(j);
            System.out.println("[" + i + "," + j + "]=" + cell.getStringCellValue());
        }
    }
Mogsdad
  • 44,709
  • 21
  • 151
  • 275

1 Answers1

-1

Your for loop should be:

for (int i = 0; i < rowsCount; i++)

not

for (int i = 0; i <= rowsCount; i++)
Jake Haller-Roby
  • 6,335
  • 1
  • 18
  • 31