title: Reading Cell Value of Excel in C#, null reference exception
body: <p>I am trying to read the cell value from an Excel sheet, and used Value and Value2 to see the Cell value. It keeps throwing "System.NullReferenceException: 'Object reference not set to an instance of an object.'" Error. I am unable to figure out where is the issue in code. </p>
  
  <p>I know the file path and the Excel sheet it's reading is correct.</p>
  
  <pre class="lang-cs prettyprint-override"><code>public String readEDriver()
  {
      int nRows = 1;
      int nCols = 1;
      String driverLoc = null;
      Excel.Application excelApp = new Excel.Application();
      if (excelApp != null)
      {
          Workbook excelWorkbook;
          excelWorkbook = excelApp.Workbooks.Open("C:\\A\\Config.xlsx", 0, true, 5, "", "", true, Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
          Worksheet excelWorksheet;
          excelWorksheet = (Excel.Worksheet)excelWorkbook.Worksheets[1];
          excelWorksheet.Activate();
          String eName =excelWorksheet.Name;
          if (excelWorksheet == null)
          {
              throw new Exception(string.Format("Named worksheet ({0}) not found.", excelWorksheet));
          }
          else  
          {
              var cellVal = ((Microsoft.Office.Interop.Excel.Range)excelWorksheet.Cells[nRows, nCols]).Value2;
  
              if (excelWorksheet.Cells[nRows,nCols]!=null)
              {
                  cellVal = ((Microsoft.Office.Interop.Excel.Range)excelWorksheet.Cells[nRows, nCols]).Value2;
                  driverLoc = cellVal.ToString();
              }
          }
  
          excelWorkbook.Close();
          excelApp.Quit();    
      }
      return driverLoc;
  }
  
  </code></pre>
  
  <p>it breaks at the</p>
  
  <pre class="lang-cs prettyprint-override"><code>var cellVal = ((Microsoft.Office.Interop.Excel.Range)excelWorksheet.Cells[nRows, nCols]).Value2;
  </code></pre>
  
  <p>It fails to retrieve the Excel Range and gives NullReferenceException</p>
  
  <p>Message: </p>
  
  <blockquote>
    <p>System.NullReferenceException : Object reference not set to an instance of an object.<br>
      Stack Trace:</p>
  </blockquote>