0

Im getting data from excel using HSSFWORKBOOK. When there is not data in the cell, it shows the error as:

Exception in thread "main" java.lang.NullPointerException at ExecuteTestcase.Testcase_execute.main(Testcase_execute.java:47)

And my code is:

Sheet guru99Sheet = file.readExcel("D:\\SampleFramework.xls");                                                                            
int rowCount = guru99Sheet.getLastRowNum()-guru99Sheet.getFirstRowNum();
for(int i=1;i<rowCount+1;i++) {
    Row row = guru99Sheet.getRow(i);
    //Check if the first cell contain a value, if yes, That means it is the new  testcase name
    if(row.getCell(0)==null) {
        //Print testcase detail on console
        System.out.println(row.getCell(1).toString()+"----"+ row.getCell(2).toString()+"----"+ row.getCell(3).toString()+"----"+ row.getCell(4).toString());
        //Call perform function to perform operation on UI
        operation.perform(allObjects, row.getCell(1).toString(), row.getCell(2).toString(), row.getCell(3).toString(), row.getCell(4).toString());
    } else {
        //Print the new testcase name when it started
        System.out.println("New Testcase->"+row.getCell(0).toString() +" Started");
    }
}

Please Help me out this.

frianH
  • 7,295
  • 6
  • 20
  • 45
arun kumar
  • 29
  • 6

1 Answers1

2

You're getting the error becuase one of your row.getCell() returns null and you've not set a Row.MissingCellPolicy.

You probably want row.getCell(1, org.apache.poi.ss.usermodel.Row.CREATE_NULL_AS_BLANK) etc.

deepy
  • 473
  • 4
  • 20
  • Thank you so much.I successfully run my code. May I know why we are using the line org.apache.poi.ss.usermodel.Row.CREATE_NULL_AS_BLANK? It will be useful for me – arun kumar Aug 26 '14 at 12:20
  • Row.CREATE_NULL_AS_BLANK sets the value to blank instead of null, null is more of a [no instance](http://stackoverflow.com/questions/2707322/what-is-null-in-java) – deepy Aug 26 '14 at 12:23