Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用java读取excel时将null更改为空_Java_Excel_Nullpointerexception_Null - Fatal编程技术网

使用java读取excel时将null更改为空

使用java读取excel时将null更改为空,java,excel,nullpointerexception,null,Java,Excel,Nullpointerexception,Null,如果我使用的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 lo

如果我使用的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();
}
}
public class-GradesDB{
公共静态void main(字符串[]args)引发异常{
//启动工作簿
File excel=新文件(“Users/Database.xlsx”);//文件位置
FileInputStream fis=新的FileInputStream(excel);
XSSF工作簿wb=新XSSF工作簿(fis);
XSSFSheet ws=wb.getSheet(“学生”);//具有空值的工作表
int rowNum=ws.getLastRowNum()+1;
int colNum=ws.getRow(0.getLastCellNum();
字符串[][]数据=新字符串[rowNum][colNum];//存储行和列
//迭代

对于(int i=0;我在条件中检查
null
if((ws.getLastRowNum()+1)==null){//do something/}else{rowNum=ws.getLastRowNum()+1;}
或使用
尝试{}catch(NullPointerException NPE){NPE.printStackTrace();}
谢谢您的评论。