title: NPOI - Get excel row count to check if it is empty
body: <p>I'm reading an <code>xlsx</code> file using <code>NPOI</code> lib, with <code>C#</code>. I need to extract some of the excel columns and save the extracted values into some kind of data structure.</p>
  
  <p>I can successfully read the file and get all the values from the 2nd (the first one contains only headers) to the last row with the following code:</p>
  
  <pre><code>...
  workbook = new XSSFWorkbook(fs);
  sheet = (XSSFSheet)workbook.GetSheetAt(0);
  ....
  int rowIndex = 1;  //--- SKIP FIRST ROW (index == 0) AS IT CONTAINS TEXT HEADERS
  while (sheet.GetRow(rowIndex) != null) {
      for (int i = 0; i &lt; this.columns.Count; i++){
         int colIndex = this.columns[i].colIndex;
         ICell cell = sheet.GetRow(rowIndex).GetCell(colIndex);
         cell.SetCellType(CellType.String);
         String cellValue = cell.StringCellValue;
         this.columns[i].values.Add(cellValue); //--- Here I'm adding the value to a custom data structure
      }
      rowIndex++;
  }
  </code></pre>
  
  <p><strong>What I'd like to do now is check if the excel file is empty or if it has only 1 row in order to properly handle the issue and display a message</strong></p>
  
  <p>If I run my code against an excel file with only 1 row (headers), it breaks on </p>
  
  <pre><code>cell.SetCellType(CellType.String); //--- here cell is null
  </code></pre>
  
  <p>with the following error:</p>
  
  <pre><code>Object reference not set to an instance of an object.
  </code></pre>
  
  <p>I also tried to get the row count with</p>
  
  <pre><code>sheet.LastRowNum
  </code></pre>
  
  <p>but it does not return the right number of rows. For example, I have created an excel with 5 rows (1xHEADER + 4xDATA), the code reads successfully the excel values. On the same excel I have removed the 4 data rows and then I have launched again the code on the excel file. <code>sheet.LastRowNum</code> keeps returning <code>4</code> as result instead of <code>1</code>.... I think this is related to some property bound to the manually-cleaned sheet cells.</p>
  
  <p>Do you have any hint to solve this issue? </p>