0

Context: Null pointer exception thrown when checking the length of blank excel cell.

Please help to fix the exception.

int lastRowNum = sh.getLastRowNum();

for (int i = 1; i < (lastRowNum+1); i++) {
    Row row = sh.getRow(i);
    System.out.println(i);
    // Null pointer exception throws for below if condition
    if (row.getCell(0).toString().length() == 0) {
        actions.operations(objectRepository, row.getCell(1).toString(), row.getCell(2).toString(), row.getCell(3).toString(), row.getCell(4).toString());

    }
    else {
        System.out.println("Test Case: " + row.getCell(0).toString() + " Started");
    }

}
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • 7
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Pradeep Simha Mar 18 '17 at 14:07

1 Answers1

0

Add null check in if condition.

if(row.getCell(0) ==null || row.getCell(0).toString().length()==0){