简体   繁体   中英

blank cell checking to avoid null pointer exception

My excel sheet is as follows:

Property_id Property_Name                               
3             Hilton                                
Season_start_date   Season_end_date MSG Sun Mon Tues Wed Thurs Fri Sat
9/16/1999            9/16/1999      fxb 100 100 100 100 100 100 100
9/16/1999            9/16/1999      fb  100 100 100 100 100 100 100
9/16/1999            9/16/1999      tfg 100 100 100 100 100 100 100

I had used following code to detect it:

            Iterator<Cell> cellIterator = row.cellIterator();

            while (cellIterator.hasNext()) {
                Cell cell = cellIterator.next();
                // Check the cell type and format accordingly

                switch (cell.getCellType()) {
                case Cell.CELL_TYPE_BLANK:
                     throw new IOException("Cell empty "+cell.getColumnIndex() + cell.getRowIndex()); 

                case Cell.CELL_TYPE_NUMERIC:
                    System.out.print(cell.getNumericCellValue() + "\t");
                    this.propertyId = cell.getNumericCellValue();
                    break;
                case Cell.CELL_TYPE_STRING:
                    System.out.print(cell.getStringCellValue() + "\t");
                    this.propertyName = cell.getStringCellValue();
                    break;
                }
            }
            ExcelToDatabase data = new ExcelToDatabase();
            if (data.existingId(propertyId) == 0){
                data.propertyDetails(propertyId, propertyName);
                data.createTable(propertyName);
            }

However this code is giving blank cell even for the cell next to property_name. Can u please tell me what should i use to throw exception when blank cell is encountered.

I had also tried :

 if (cell == null)
    {
       throw new IOException("blank cell ");  

    }

But this does not work.. No exception is thrown

I dont know why you wanted to throw exception whenever you encountered blank cell. I guess your problem might be solved if you ignored blank cells.

switch (cell.getCellType()) {
            case Cell.CELL_TYPE_NUMERIC:
                System.out.print(cell.getNumericCellValue() + "\t");
                this.propertyId = cell.getNumericCellValue();
                break;
            case Cell.CELL_TYPE_STRING:
                System.out.print(cell.getStringCellValue() + "\t");
                this.propertyName = cell.getStringCellValue();
                break;
            default;
                break;

            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM