Java 当访问具有该值的单元格时,我遇到空指针异常?

Java 当访问具有该值的单元格时,我遇到空指针异常?,java,apache-poi,Java,Apache Poi,我正在使用ApachePOI库从EXCEL工作表中获取数据。我已经附上了突出显示黄色部分的EXCEL表。我试图从EXCEL工作表中提取所有数据,但没有从突出显示的部分获取数据。当试图访问这些单元格时,它会出现空指针异常 样本文件: 示例代码。 FileInputStream inputStream=新的FileInputStream(文件) Workbook工作簿=新XSSF工作簿(inputStream); Sheet firstSheet=工作簿。getSheetAt(0); 迭代器迭代器

我正在使用ApachePOI库从EXCEL工作表中获取数据。我已经附上了突出显示黄色部分的EXCEL表。我试图从EXCEL工作表中提取所有数据,但没有从突出显示的部分获取数据。当试图访问这些单元格时,它会出现空指针异常

样本文件:

示例代码。 FileInputStream inputStream=新的FileInputStream(文件)

Workbook工作簿=新XSSF工作簿(inputStream);
Sheet firstSheet=工作簿。getSheetAt(0);
迭代器迭代器=firstSheet.Iterator();
while(iterator.hasNext()){
行nextRow=iterator.next();
迭代器cellIterator=nextRow.cellIterator();
while(cellIterator.hasNext()){
Cell=cellIterator.next();
System.out.print(cell.getStringCellValue());
系统输出打印(“,”);
}
System.out.println();
}
workbook.close();
inputStream.close();

当您运行上述程序时,您将得到一些未从excel表中提取的字段(突出显示的部分)。当您显式尝试访问这些单元格时,将出现空指针异常。

无法重现该行为。If
System.out.print(cell.getStringCellValue())
抛出一个NPE,然后
单元格
必须为
null
。但是,根据您的代码,
单元格
不能为
null
,因为
行。cellIterator
只对存在且不存在的单元格进行迭代

已下载您的
SAMPLE.xlsx
,并使用了中的代码。此代码读取所有单元格时不会出现问题

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.*;

import java.io.FileInputStream;

class ReadExcelExampleDataFormatter {

 public static void main(String[] args) throws Exception {

  Workbook wb  = WorkbookFactory.create(new FileInputStream("SAMPLE.xlsx"));

  DataFormatter formatter = new DataFormatter();
  FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();

  Sheet sheet = wb.getSheetAt(0);
  for (Row row : sheet) {
   for (Cell cell : row) {
    CellReference cellRef = new CellReference(row.getRowNum(), cell.getColumnIndex());
    System.out.print(cellRef.formatAsString());
    System.out.print(" - ");
    // get the text that appears in the cell by getting the cell value and applying any data formats (Date, 0.00, 1.23e9, $1.23, etc)
    String text = formatter.formatCellValue(cell);
    System.out.println(text);
   }
  }
  wb.close();
 }
}
部分结果(您的第一个黄色范围):


可能重复@JoeC我知道什么是NullPointerException。请仔细阅读这个问题。该单元格包含字符串值,但当我试图访问它时,它会出现空指针异常。当单元格不包含该值时,ApachePOI给出空指针异常。哪一代码行抛出NPE?请显示stacktrace.Iterator Iterator=firstSheet.Iterator();while(iterator.hasNext()){Row nextRow=iterator.next();for(int i=0;i<4;i++){Cell Cell Cell=nextRow.getCell(CellReference.convertColStringToIndex(column[i]);System.out.print(Cell.getStringCellValue());System.out.print(“,”;)workbook.close();inputStream.close();===============================这是显式访问单元格的程序。我正在获取NPE。您可以编辑答案以添加代码。这样对每个人都会更方便。
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.*;

import java.io.FileInputStream;

class ReadExcelExampleDataFormatter {

 public static void main(String[] args) throws Exception {

  Workbook wb  = WorkbookFactory.create(new FileInputStream("SAMPLE.xlsx"));

  DataFormatter formatter = new DataFormatter();
  FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();

  Sheet sheet = wb.getSheetAt(0);
  for (Row row : sheet) {
   for (Cell cell : row) {
    CellReference cellRef = new CellReference(row.getRowNum(), cell.getColumnIndex());
    System.out.print(cellRef.formatAsString());
    System.out.print(" - ");
    // get the text that appears in the cell by getting the cell value and applying any data formats (Date, 0.00, 1.23e9, $1.23, etc)
    String text = formatter.formatCellValue(cell);
    System.out.println(text);
   }
  }
  wb.close();
 }
}
A15 - Report
Summary
B15 - Real Estate
C15 - Count
D15 - 3
E15 - 0
F15 - 2
A16 - 
B16 - 
C16 - Balance
D16 - $94,263.00
E16 - $0.00
F16 - $94,263.00
A17 - 
B17 - 
C17 - Current
D17 - 2
E17 - 0
F17 - 2
A18 - 
B18 - 
C18 - Delinquent
D18 - 0
E18 - 0
F18 - 0