空指针exceptionapache poi

嗨,我们一直在阅读xls和xlsx文件使用Apache poi我们的Java程序,问题是我们得到空指针exception有两个原因。第一个是我们已经解决了空白单元格,另一个是当我们正在select一个没有任何logging的列

我们的程序请求excel文件的path,然后是文件的具体图纸编号以及要读取的图纸的特定列号。这里是读取xls文件的代码

public void readXLSFile()throws IOException{ InputStream ExcelFileToRead = new FileInputStream(path); HSSFWorkbook wb = new HSSFWorkbook(ExcelFileToRead); HSSFSheet sheet=wb.getSheetAt(sheetname); HSSFRow row; HSSFCell cell; Iterator rows = sheet.rowIterator(); list1.clear(); while (rows.hasNext()) { headers.clear(); row=(HSSFRow) rows.next(); // Iterator cells = row.cellIterator(); headers.add("contents"); cnt = cnt+1; cell = row.getCell(cols); if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING) { //System.out.println(cell.getStringCellValue()+"(string)"); list.add(cell.getStringCellValue()); d.add(cell.getStringCellValue()); list1.add(new KeyValuePair(cell.getStringCellValue(),"")); } else if(cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) { //System.out.println(cell.getNumericCellValue()+"(numeric)"); double num = cell.getNumericCellValue(); String num2 = String.valueOf(num); list.add(num2); d.add(num2); list1.add(new KeyValuePair(num2,"")); } else if(cell.getCellType() == HSSFCell.CELL_TYPE_BOOLEAN) { //System.out.println(cell.getBooleanCellValue()+"(boolean)"); String bool = String.valueOf(cell.getBooleanCellValue()); list.add(bool); d.add(bool); list1.add(new KeyValuePair(bool,"")); } else { //U Can Handel Boolean, Formula, Errors } //System.out.println(); } arrey = list.toArray(new String[list.size()]); data.add(d); // System.out.println(data); model = new DefaultTableModel(); table_1.setModel(model); table_1.setModel(model); model.setColumnIdentifiers(new String[] {"row","contents"}); for (KeyValuePair p : list1){ int nor=table_1.getRowCount(); int n2 = nor +1; n1 = Integer.toString(n2); // model.addColumn(new String [] {n1}); model.addRow(new String[] {n1,p.key, p.value}); } // model.addColumn(new String[] {n1}); } 

可变图纸名称是Excel文件的图纸编号

 HSSFSheet sheet=wb.getSheetAt(sheetname); 

而variablescols是您想要读取的特定列

 cell = row.getCell(cols); 

我们可以阅读每张纸的第一列,也是第二张纸的第二列,但是当我编辑我的testing文件时,程序现在只能读取每张纸的第一列..错误是空指针exception..你可以帮助提前感谢

雷耶斯,

问题是,你永远不会testing单元格是否为空!

 if (cell == null) { System.out.println("Cell is Empty in Column:" + cols); } else if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING) { //code } 

一般来说,在处理Cell.getCellType()函数时应该小心,因为空单元可以是null或是CELL_TYPE_BLANK

我希望它有帮助。

这是我的方式来避免Cell NullPoiterException。
你可以试试看 祝你好运!

 /** * Get string value of {@link Cell} object * * @param cell * {@link Cell} object * @return String value of {@link Cell} object */ private static String getCellValueString(Cell cell) { String value=""; if(cell!=null) { switch(cell.getCellType()){ case Cell.CELL_TYPE_BOOLEAN: value=String.valueOf(cell.getBooleanCellValue()); break; case Cell.CELL_TYPE_NUMERIC: value=BigDecimal.valueOf( cell.getNumericCellValue()).toPlainString(); break; case Cell.CELL_TYPE_STRING: value=String.valueOf(cell.getStringCellValue()); break; case Cell.CELL_TYPE_FORMULA: value=String.valueOf(cell.getCellFormula()); break; case Cell.CELL_TYPE_BLANK: value=""; break; } } else { logger.error("Cell is null"); } return value.trim(); }