0

I have a multi-lingual Sitecore site and in one of the page views, a single string property of the model is always throwing NullRefEx, even when it has a value.

   <span class="category">@Model.Category</span> <!-- works as usual -->
   <h1>@Model.Name</h1> <!-- throws NullRefEx on any value -->

I've put a breakpoint where I set the Name value and inside the view. In both cases, the property has a string value. What is even stranger is that Model.Category is fine rendering even with empty string value, only Model.Name complains. What is even stranger is that Model.Name complains only when I change the page language from the default to another one. I even tried putting null checks in the view and assigning a value to Name through the immediate window - it keeps throwing NullRef.

I tried restarting VS2012 and rebuilding the project.

Here is the exception

[NullReferenceException: Object reference not set to an instance of an object.]
ASP._Page_Views_ProjectName_Details_cshtml.Execute() in c:\<top secret stuff right here>\Details.cshtml:38
System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +252
System.Web.Mvc.WebViewPage.ExecutePageHierarchy() +160
System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +103
System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +362
System.Web.Mvc.<>c__DisplayClass1a.<InvokeActionResultWithFilters>b__17() +30
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation) +416
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation) +416
System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +398

EDIT: How is the other question answering mine? I don't see any case that fits mine. Clearly my Model is not null (Category renders fine). Also, my Name property has a string value when I put a breakpoint on it. Adding null-checks on both Model and Model.Name did nothing - I still got NullRefEx.

Pavel S
  • 68
  • 8
  • @Html.DisplayFor(model => model.name) – Sachu Mar 02 '17 at 11:30
  • @Sachu No, it doesn't work. – Pavel S Mar 02 '17 at 11:34
  • Same exception. And again, no InnerException. I am not even sure that DisplayFor works on Sitecore MVC. I've used it only on pure MVC forms before. – Pavel S Mar 02 '17 at 11:41
  • If Model really is not null and Name is a simple property getter that is returning a value then you are looking in the wrong place. A null reference exception is thrown as described in the duplicate only when you attempt to do something to a null. If you are 100% sure nothing on that line is null then it is not throwing the exception there. – Chris Mar 02 '17 at 11:44
  • @Chris I think you solved it. I populated all other fields for the other language in sitecore and the error disappeared. I still don't get why the stack trace was claiming it was coming from the line with Model.Name. Even when I was stepping through the view, it was raised on that line. Very bizarre. – Pavel S Mar 02 '17 at 11:56

0 Answers0