简体   繁体   中英

Null Reference Exception and the ?? Operator

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

看起来Model或Evt为null,实际上不是Description字段。

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

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 EntitySet s 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM