2

How can I handle an empty cell using poi jars? This is the code:

public static String getdata(String SheetName, String ObjectLabel, int row) throws InvalidFormatException, IOException{
    String objvalue = null;
    FileInputStream fi = new FileInputStream(dir + "\\src\\pack\\TaurusRefactored\\TaurusRegistration1.xls");
    Workbook w = WorkbookFactory.create(fi);
    Sheet s = w.getSheet(SheetName);
    int rowcount = s.getLastRowNum();
    int columncount = s.getRow(0).getLastCellNum();
    for (int i = 0; i < columncount; i++) {
        String objlabl = s.getRow(0).getCell(i).getStringCellValue();
        if (objlabl.equals(ObjectLabel)) {
            switch (s.getRow(row).getCell(i).getCellType()) {
            case Cell.CELL_TYPE_STRING:
                objvalue = s.getRow(row).getCell(i).getStringCellValue().trim();
                break;
            case Cell.CELL_TYPE_NUMERIC:
                objvalue = Integer.toString((int) s.getRow(row).getCell(i).getNumericCellValue());
                break;
            case Cell.CELL_TYPE_BLANK:
                objvalue = "";
            default:
                break;
            }
        }
    }
     return objvalue;
}

I am getting the error the switch statement: (s.getRow(row).getCell(i).getCellType()) while taking the data from an empty cell.

ApproachingDarknessFish
  • 14,133
  • 7
  • 40
  • 79
kranthi kumar
  • 176
  • 1
  • 3
  • 5
  • What does this have to do with Selenium?!?! – SiKing Nov 21 '14 at 21:57
  • possible duplicate of [How to get an Excel Blank Cell Value in Apache POI?](http://stackoverflow.com/questions/4929646/how-to-get-an-excel-blank-cell-value-in-apache-poi) – Gagravarr Nov 21 '14 at 22:25

1 Answers1

1

You just return cell null and blank value then read it.

Cell cell = row.getCell(cn, Row.RETURN_NULL_AND_BLANK);
if ((cell == null) || (cell.equals("")) || (cell.getCellType() == cell.CELL_TYPE_BLANK))
Gagravarr
  • 47,320
  • 10
  • 111
  • 156
RDY
  • 613
  • 1
  • 9
  • 25