我有一个偶尔报告 NullReferenceException 的 MVC3 应用程序。到目前为止,我还无法重现该问题。下面是该函数的编辑版本(以保护无辜者;)以及我在抛出 NullReferenceException 时得到的相应堆栈输出。
[HttpPost]
public ActionResult DoSomething(FormCollection collection)
{
if (null == collection)
{
return RedirectToAction("Index", "Home");
}
// other work occurs...
// NOTE: no problem here accessing collection
QcModel qcModel = new QcModel (Int32.Parse(collection["Id"]), collection["Result"]);
try
{
Process(qcModel); // Process() throws some kind of exception
}
catch (Exception ex)
{
TempData["ValidationError"] = ex.Message;
// next line is the one that reports the NullReferenceException in stack, aka line 254
return RedirectToAction("Order", new { Id = Int32.Parse(collection["Id"]), result = collection["Result"], reserved = 0 });
}
// additional work if we make it here...
}
到目前为止,基本上我所推断的是,当我们到达我的 catch{} 中突出显示的行时,集合对象为空。现在我无法证明这一点,因为我无法重现这个特定的错误,但我相信这是正在发生的事情,因为在 catch 中生成的异常会冒泡到更高级的处理程序,该处理程序会发送电子邮件给我这个堆栈输出:
Exception: System.NullReferenceException
Error Message: Object reference not set to an instance of an object.
Stack: at MyWebsite.Controllers.MyController.DoSomething(FormCollection collection) in C:\MyWebsite\Controllers\MyController.cs:line 254
at lambda_method(Closure , ControllerBase , Object[] )
at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass15.<InvokeActionMethodWithFilters>b__12()
at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation)
at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass15.<>c__DisplayClass17.<InvokeActionMethodWithFilters>b__14()
at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation)
at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass15.<>c__DisplayClass17.<InvokeActionMethodWithFilters>b__14()
at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList`1 filters, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
at System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName)
at System.Web.Mvc.Controller.ExecuteCore()
at System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext)
at System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext)
at System.Web.Mvc.MvcHandler.<>c__DisplayClass6.<>c__DisplayClassb.<BeginProcessRequest>b__5()
at System.Web.Mvc.Async.AsyncResultWrapper.<>c__DisplayClass1.<MakeVoidDelegate>b__0()
at System.Web.Mvc.Async.AsyncResultWrapper.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.End()
at System.Web.Mvc.MvcHandler.<>c__DisplayClasse.<EndProcessRequest>b__d()
at System.Web.Mvc.SecurityUtil.<GetCallInAppTrustThunk>b__0(Action f)
at System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Action action)
at System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult)
at System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result)
at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)