如果我使用具有空值的excel表,则此代码会出现“java.lang.NullPointerException”错误。有没有办法将空值更改为空白?谢谢。
public class GradesDB {
public static void main (String[] args) throws Exception {
//initiating workbook
File excel = new File("Users/Database.xlsx"); //file location
FileInputStream fis= new FileInputStream(excel);
XSSFWorkbook wb = new XSSFWorkbook(fis);
XSSFSheet ws = wb.getSheet("Students"); //sheet with null value
int rowNum = ws.getLastRowNum() +1;
int colNum = ws.getRow(0).getLastCellNum();
String[][] data = new String[rowNum][colNum]; //storing row and col
//iteration
for (int i =0; i <rowNum; i++) {
XSSFRow row = ws.getRow(i);
for (int j = 0; j <colNum; j++){
XSSFCell cell = row.getCell(j);
String value = cellToString(cell);
data[i][j] = value;
System.out.println(value);
}
}
//System.out.println(data[4][3]);
}
public static String cellToString(XSSFCell cell) {
int type;
Object result = null;
type = cell.getCellType();
switch (type) {
case 0 :
result = cell.getNumericCellValue(); //numeric value
break;
case 1 :
result = cell.getStringCellValue(); //string value
break;
case 2 :
result = cell.getBooleanCellValue();
break;
}
return result.toString();
}
}
我不确定问题出在哪里。