DefaultDisplayMode.CanHandleContext throws null reference exception

Hi,

I have recently added .Mobile.cshtml view to my ASP.net mvc 5 website’s home page. Now i am getting the following error on live website which has about 200 daily visitors, both desktop and mobile users. I am unable to reproduce this error on my local machine
nor on the staging website. Has anybody seen a similar exception and know what might be the cause for this error. Below is the stacktrace of the application. 

Error Message: Object reference not set to an instance of an object. 

Stack Trace: at TourPlanner.Web.MvcApplication.b__1(HttpContextBase context) at 
System.Web.WebPages.DefaultDisplayMode.CanHandleContext(HttpContextBase httpContext) at
System.Web.WebPages.DisplayModeProvider.d__4.MoveNext() at 
System.Web.Mvc.VirtualPathProviderViewEngine.GetPath(ControllerContext controllerContext, 
String[] locations, String[] areaLocations, String locationsPropertyName, String name, String 
controllerName, String cacheKeyPrefix, Boolean useCache, String[]& searchedLocations) at 
System.Web.Mvc.VirtualPathProviderViewEngine.FindView(ControllerContext controllerContext, String 
viewName, String masterName, Boolean useCache) at System.Web.Mvc.ViewEngineCollection.
<>c__DisplayClass6.b__4(IViewEngine e) at System.Web.Mvc.ViewEngineCollection.Find(Func`2 lookup, 
Boolean trackSearchedPaths) at System.Web.Mvc.ViewEngineCollection.FindView(ControllerContext 
controllerContext, String viewName, String masterName) at 
System.Web.Mvc.ViewResult.FindView(ControllerContext context) at 
System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) at 
System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, 
ActionResult actionResult) at 
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 
filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult 
actionResult) at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 
filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext 
controllerContext, ActionResult actionResult) at 
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext 
controllerContext, IList`1 filters, ActionResult actionResult) at 
System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass1e.<>c__DisplayClass28.b__19() 
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass1e.b__1b(IAsyncResult 
asyncResult)

Regards

Hi,

One of the possibility: There are one or more non nullable fields contain null on the following fields:

String[] locations, String[] areaLocations, String locationsPropertyName, String name, String 
controllerName, String cacheKeyPrefix, Boolean useCache, String[]& searchedLocations) at 
System.Web.Mvc.VirtualPathProviderViewEngine.FindView(ControllerContext controllerContext, String 
viewName, String masterName, Boolean useCache) at System.Web.Mvc.ViewEngineCollection.
<>c__DisplayClass6.b__4(IViewEngine e) at System.Web.Mvc.ViewEngineCollection.Find(Func`2 lookup, 
Boolean trackSearchedPaths) at System.Web.Mvc.ViewEngineCollection.FindView(ControllerContext 
controllerContext, String viewName, String masterName)

This can happened when editing or create an entity. You may need to check null value and set default value when user does not fill a field or send view back to it’s create or edit view.

Hope can help.

sajid Mushtaq

Stack Trace: at TourPlanner.Web.MvcApplication.b__1(HttpContextBase context) at

Are you calling this outside ASP.NET request thread(may be in worker thread)

imran_ku07

sajid Mushtaq

Stack Trace: at TourPlanner.Web.MvcApplication.b__1(HttpContextBase context) at

Are you calling this outside ASP.NET request thread(may be in worker thread)

No, This code is generated by MVC using reflection. :(

Leave a Reply