On one of my ASP.NET MVC websites I occassionally get a NPE in production that I'm having trouble understanding root cause. I'm using ELMAH for tracing errors.
The full stacktrace is here (not much to it:
System.NullReferenceException: Object reference not set to an instance of an object.
at PG.Controllers.FooController.FooActionName(FooModel model) in
{REMOVED}\web\www\Controllers\FooController.cs:line 622
Here's line 622:
if(UserCanEditFoo(out result))
Here's the "UserCanEditFoo listing:
private bool UserCanEditFoo(out ActionResult result)
{
String email = User != null && User.Identity != null ? User.Identity.Name : String.Empty;
UserAccount user = String.IsNullOrEmpty(email) ? null : this.accountServices.GetUserAccount(email);
bool canEditFoo = false;
result = null;
// Don't know this user
if(user == null)
{
FormsAuthentication.SignOut();
Session.Abandon();
result = RedirectToAction(Routes.Actions.Index, Routes.Controllers.Home);
}
// Only admins and foo profile can edit foo
else if(user.Role != Types.UserRole.Admin && user.Role != Types.UserRole.Foo)
{
result = new HttpUnauthorizedResult("Oops! Only administrators and foo users can edit foo.");
}
else
{
result = null;
canEditFoo = true;
}
return canEditFoo;
}
I'm assuming the exception is actually raised in the "UserCanEditFoo" private method but I don't understand why the line number is not being reported as such. On the flipside, if the exception is truly being raised at line 622 I don't understand how that can be given it is a simple function call.
Any help will be much appreciated. Thanks in advance.