tags:

views:

363

answers:

4

I have a form within my Asp.Net MVC application which returns a Null reference exception at the following line, but only after postback errors:

<%= Html.TextArea("Description", Model.Evt.Description ?? "")%>

The string Description is indeed null, but my expectation was that if Model.evt.Description is null then "" will be shown in the TextArea. Why is it doing this when it works fine on initially loading the form?

The only difference is that the type of ViewUserControl is is populated from a new object for initial load and the model object on postback reload.

What am I missing?

The Stack Trace is as follows:

[NullReferenceException: Object reference not set to an instance of an object.]
   System.Web.Mvc.HtmlHelper.GetModelStateValue(String key, Type destinationType) +63
   System.Web.Mvc.Html.TextAreaExtensions.TextAreaHelper(HtmlHelper htmlHelper, String name, Boolean useViewData, String value, IDictionary`2 implicitAttributes, IDictionary`2 explicitParameters, IDictionary`2 htmlAttributes) +261
   System.Web.Mvc.Html.TextAreaExtensions.TextArea(HtmlHelper htmlHelper, String name, String value, IDictionary`2 htmlAttributes) +213
   System.Web.Mvc.Html.TextAreaExtensions.TextArea(HtmlHelper htmlHelper, String name, String value) +61
   ASP.views_events_eventform_ascx.__Render__control1(HtmlTextWriter __w, Control parameterContainer) in c:\Documents and Settings\Richard Box\My Documents\Visual Studio 2008\Projects\EventsManager\EventsManager\Views\Events\EventForm.ascx:64
   System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +256
   System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +19
   System.Web.UI.Control.Render(HtmlTextWriter writer) +10
   System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +27
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +99
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25
   System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +134
   System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +19
   System.Web.UI.Page.Render(HtmlTextWriter writer) +29
   System.Web.Mvc.ViewPage.Render(HtmlTextWriter writer) +59
   System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +27
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +99
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1266
+9  A: 

It looks like either Model or Evt is null, not actually the Description field.

Joseph
Unfortunately not, unless I am mistaken. I have placed a breakpoint inside the EventFormModel constructor and in both cases the Evt object has properties with values in. Also in ViewData.Model when I step past the return View(new EventFormViewModel(evt)); line the Evt object is populated. This is the last point that I can check I believe.
Richbits
Well just for debugging you can change it to Model == null ? "null model : (Model.Evt == null ? "null evt" : (Model.Evt.Description == null ? "null description" : Model.Evt.Description))and that will tell you what is actually null.
David
David, Thanks, I think that Sterno might be on to something, but still trying to work through. I replaced Model.Evt.Description in the Html.TextArea to include the statement above.Whilst on first load I did see "null description" populate the text box (correctly), on post back the same failure occurred.
Richbits
+1  A: 

Is it possible that Evt is null as well? That would account for the exception

JaredPar
Thanks, pretty sure it is populated... see comment on previous answer.
Richbits
A: 

It doesn't look like it has anything to do with the ?? operator.

Are you calling ModelState.SetModelValue() for the description? There seems to be a similar question asked and answered here.

Sterno
You may have a point here, I am using a custom model binder, which does use ModelState.SetModelValue(), but it is raising an exception before reaching that point for the decription. I'll take a better look at the article tomorrow.
Richbits
A: 

My mistake, but I would say that the error information was pretty poor in this case, and I have no idea why this line was highlighted...

Within my Custom Model Binder I create an Event object and populate it with information from the form, however because the Event object is composed of several other objects some of which are EntitySets I need to ensure that each of these are constructed before trying to populate them.

I should have noticed that this was not the case, and that an exception was raised in the model binder which is caught in the controller. The object and property which was raising the exception was completely unrelated to the Model.Evt.Description so I have no idea why this was highlighted in the error on displaying the view.

Richbits