Can't understand why I'm getting this nullReference exception in ASP MVC when loading a page

asp.net-mvc, nullreferenceexception, web

Solution

I run into the same issue. Unfortunately the stack trace isn't very helpful in this case. The exception is happening for sure in the view, what it was in my case:

@Model.SomeProperty.Trim()

And Model.SomeProperty was null. As there is no way how to debug it; I recommend binary split approach:

- save your view somewhere else

- remove one-by-one logical parts from your view

- after each removal save your view and retest

- if the exception is no longer reproducible, the NullReferenceException happened in the block you've removed; so add it back and remove logical parts inside this block until you isolate the line causing an exception

Its lame I know, but I found no other way - btw. this is what I got for doing controller job in view.

Problem

I have a web application that is not giving me any problems on my computer, but when I deploy it to the server I get this error. It doesn't give me any source code lines to check and I don't have any idea why it may be happening ``` Exception Details: System.NullReferenceException: Object reference not set to an instance of an object. Source Error: [No relevant source lines] Source File: c:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\root\a9dc635c\ccb3814\App_Web_item.cshtml.f0356b3c.uyw8roer.0.cs Line: 0 Stack Trace: [NullReferenceException: Object reference not set to an instance of an object.] ASP._Page__AssemblyResource_MyProject_Web_Mvc__Version_1_0_0_0__Culture_neutral__PublicKeyToken_null_Views_LeagueGlobal_Item_cshtml.Execute() in c:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\root\a9dc635c\ccb3814\App_Web_item.cshtml.f0356b3c.uyw8roer.0.cs:0 System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +279 System.Web.Mvc.WebViewPage.ExecutePageHierarchy() +125 System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +195 System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +383 System.Web.Mvc.<>c__DisplayClass1a.<InvokeActionResultWithFilters>b__17() +32 System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation) +977396 System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation) +977396 System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) +265 System.Web.Mvc.Async.<>c__DisplayClass25.<BeginInvokeAction>b__22(IAsyncResult asyncResult) +964636 System.Web.Mvc.<>c__DisplayClass1d.<BeginExecuteCore>b__18(IAsyncResult asyncResult) +28 System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +20 System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +67 System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +20 System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +53 System.Web.Mvc.<>c__DisplayClass8.<BeginProcessRequest>b__3(IAsyncResult asyncResult) +42 System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +20 System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +53 System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +469 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +375 ```

Original source