object reference not set to an instance of an object

Jun 13, 2013 at 4:45 PM
Edited Jun 13, 2013 at 4:46 PM
Hello I have following bit of code used with the excel/csv sheets to import. I am using code plex excel dll for this purpose.
        Dim _stream As FileStream = File.Open(Server.MapPath("~/admin/DBDesigner/tempImportFolder/" & Fname),                            FileMode.Open, FileAccess.Read, FileShare.ReadWrite)
      Dim excelReader As IExcelDataReader = ExcelReaderFactory.CreateBinaryReader(_stream)
      excelReader.IsFirstRowAsColumnNames = FirstRowCols
      Dim dtTemp As DataTable = excelReader.AsDataSet().Tables(0)

When the control is coming to last line of the code it is throwing the exception

-->object reference not set to an instance of an object

Please help me out

Thanks in advance
Developer
Jun 13, 2013 at 5:20 PM
excelReader looks good because it has a property set on the line before, that only leaves .Tables(0) as the candidate for null.

Can you check
Tables isnot nothing
and also
tables.count

Then go through the tables returned and check if each element is not null.

Can't see without a sample document really, but it might be that the first set of tables or sheet is just missing.
Jun 13, 2013 at 5:31 PM
You might want to try the equivalent of:
        foreach (DataTable sheet in ds.Tables)
        {
And see what tables you get or if you still get errors.
Jun 14, 2013 at 8:32 AM
Hi Ryan and Steve,

Thanks for the reply.

Actually my excel sheet is having the data in it and even it is throwing the same exception.


Thanks for the help
Developer
Jun 14, 2013 at 9:17 AM
Can you post the full stack trace?
Dec 6, 2013 at 3:36 PM
I had the same problem. My Excel spreadsheet was corrupt even though it opened in Excel after I received the "Excel cannot open the file [filename] because the file format or file extension is not valid." message. I resaved the file and no more errors.
Mar 24, 2014 at 4:27 PM
Edited Mar 24, 2014 at 4:29 PM
Hi, I am facing issue with reading .xls and .xlsx file. It does work in my local machine. But, when published to QA box it throws the below error. Did anyone came across this issue ? I have pasted below the error stack trace for the reference.

My code :

FileOperation.FileStream stream = FileOperation.File.Open(path, FileOperation.FileMode.Open, FileOperation.FileAccess.ReadWrite);
        IExcelDataReader excelReader = null;

        if (fileType.FileExtension == "xls" && new List<string>() { "application/excel", "application/vnd.ms-excel", "application/x-excel", "application/x-msexcel" }.Contains(fileType.FileMimeType))
        {
            excelReader = ExcelReaderFactory.CreateBinaryReader(stream);
        }
        else if (fileType.FileExtension == "xlsx" && fileType.FileMimeType == "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
        {
            excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
        }

        excelReader.IsFirstRowAsColumnNames = false;

        List<FeeScheduleLine> feeScheduleLines = new List<FeeScheduleLine>();

        while (excelReader.Read())
        {

        }
Thanks in advance.


<b>Stack Trace:</b> <br><br>
        <table width=100% bgcolor="#ffffcc">
           <tr>
              <td>
                  <code><pre>
[NullReferenceException: Object reference not set to an instance of an object.]
Emds.UI.Razor.Areas.Bill.Controllers.FeeScheduleController.ReadExcelFile(String path, FileType fileType) in e:\workspace\CreateDailyCode_PM_NavTest\MiddleTier\Razor\Emds.UI.Razor\Areas\Bill\Controllers\FeeScheduleController.cs:616
Emds.UI.Razor.Areas.Bill.Controllers.FeeScheduleController.UploadFile() in e:\workspace\CreateDailyCode_PM_NavTest\MiddleTier\Razor\Emds.UI.Razor\Areas\Bill\Controllers\FeeScheduleController.cs:470
lambda_method(Closure , ControllerBase , Object[] ) +62
System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +14
System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary2 parameters) +214
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary
2 parameters) +27
System.Web.Mvc.<>c__DisplayClass15.<InvokeActionMethodWithFilters>b__12() +55
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func1 continuation) +253
System.Web.Mvc.&lt;&gt;c__DisplayClass17.&lt;InvokeActionMethodWithFilters&gt;b__14() +21
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func
1 continuation) +253
System.Web.Mvc.<>c__DisplayClass17.<InvokeActionMethodWithFilters>b__14() +21
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList1 filters, ActionDescriptor actionDescriptor, IDictionary2 parameters) +191
System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +324
System.Web.Mvc.Controller.ExecuteCore() +106
System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +91
System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +10
System.Web.Mvc.<>c__DisplayClassb.<BeginProcessRequest>b__5() +34
System.Web.Mvc.Async.<>c__DisplayClass1.<MakeVoidDelegate>b__0() +19
System.Web.Mvc.Async.<>c__DisplayClass81.&lt;BeginSynchronous&gt;b__7(IAsyncResult _) +10

System.Web.Mvc.Async.WrappedAsyncResult
1.End() +62
System.Web.Mvc.<>c__DisplayClasse.<EndProcessRequest>b__d() +48
System.Web.Mvc.SecurityUtil.<GetCallInAppTrustThunk>b__0(Action f) +7
System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Action action) +22
System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +60
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9

System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9514928

System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155
</pre></code>
              </td>
           </tr>
        </table>

        <br>

        <hr width=100% size=1 color=silver>

        <b>Version Information:</b>&nbsp;Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.18446

        </font>

</body>
</html>
<!-- [NullReferenceException]: Object reference not set to an instance of an object. at Emds.UI.Razor.Areas.Bill.Controllers.FeeScheduleController.ReadExcelFile(String path, FileType fileType) in e:\workspace\CreateDailyCode_PM_NavTest\MiddleTier\Razor\Emds.UI.Razor\Areas\Bill\Controllers\FeeScheduleController.cs:line 616 at Emds.UI.Razor.Areas.Bill.Controllers.FeeScheduleController.UploadFile() in e:\workspace\CreateDailyCode_PM_NavTest\MiddleTier\Razor\Emds.UI.Razor\Areas\Bill\Controllers\FeeScheduleController.cs:line 470 at lambda_method(Closure , ControllerBase , Object[] ) at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass15.<InvokeActionMethodWithFilters>b__12() at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass15.<>c__DisplayClass17.<InvokeActionMethodWithFilters>b__14() at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass15.<>c__DisplayClass17.<InvokeActionMethodWithFilters>b__14() at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList`1 filters, ActionDescriptor actionDescriptor, IDictionary`2 parameters) at System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) at System.Web.Mvc.Controller.ExecuteCore() at System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) at System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) at System.Web.Mvc.MvcHandler.<>c__DisplayClass6.<>c__DisplayClassb.<BeginProcessRequest>b__5() at System.Web.Mvc.Async.AsyncResultWrapper.<>c__DisplayClass1.<MakeVoidDelegate>b__0() at System.Web.Mvc.Async.AsyncResultWrapper.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _) at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.End() at System.Web.Mvc.MvcHandler.<>c__DisplayClasse.<EndProcessRequest>b__d() at System.Web.Mvc.SecurityUtil.<GetCallInAppTrustThunk>b__0(Action f) at System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Action action) at System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) at System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) --><!-- This error page might contain sensitive information because ASP.NET is configured to show verbose error messages using &lt;customErrors mode="Off"/&gt;. Consider using &lt;customErrors mode="On"/&gt; or &lt;customErrors mode="RemoteOnly"/&gt; in production environments.-->