Excel 파일을 읽으려면 org.apache.poi.ss.usermodel의 XSSFWorkbook을 사용하려고합니다. 어떤 이유로 Sheet 메서드 중 하나에서 데이터를 가져 오려고 할 때 널 포인터 예외가 발생합니다. 나는 자바 프로그래밍에 익숙하지 않고 매우 근본적인 것일 수도있다.XSSFWorkbook 시트 인터페이스를 사용하면 Java Null 포인터 예외가 발생합니다.
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());
}
}
하지? 그것이 던지는 선을 강조하는 것은 역시 도움이 될 것입니다. – Pedantic
널 포인터 예외가 'int rowsCount ...'라인에서 발생했습니다. – user3334428
그러면 workbook.getSheetAt (0)은 null을 반환합니다. 왜 그런가? – Pedantic