2

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.

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
Chad
  • 422
  • 1
  • 3
  • 17
  • In your `else if` are you sure that the `user` or `user.role` are not null? – DROP TABLE users Jan 06 '14 at 15:58
  • 2
    @BitsEvolved: can you debug this code and break on the exception? Or get a dump file that would help you diagnose it? – Dan Puzey Jan 06 '14 at 16:00
  • 1
    @DROPtableusers, `user`, at least, will not be null at the `else if` conditional, because the `if` conditional is checking for that. – Brian S Jan 06 '14 at 16:05
  • @BrianS true. Guess the `user.role` would be the only thing that would be causing an error at that spot then. – DROP TABLE users Jan 06 '14 at 16:08
  • try remove `session.abandon` line, also are you sure that `this.accountServices` not a `null`? – Grundy Jan 06 '14 at 16:18
  • @DanPuzey I cannot debug the code given the production environment where the site is hosted. But I'll look into getting a dump file (not sure how to go about that -- will research). Thanks for the suggestion. – Chad Jan 06 '14 at 16:33
  • @Grundy I'm not sure of anything so I'll add a few more != null checks and see where that takes me. I don't like shotgunning fixes but this is safe and the issue crops up often enough that I'll know if this is root cause. Thanks for the suggestion. – Chad Jan 06 '14 at 16:36

1 Answers1

0

Assuming this.accountServices cannot be null, your code looks fine to me, so I would suggest that the exception is actually happening elsewhere (perhaps due to inlining).

Take a look at this question for details on displaying correct line numbers in Release mode.

Community
  • 1
  • 1
Rob
  • 1,472
  • 12
  • 24
  • Thanks for the suggestion but I've already made those changes (unless I missed a step). I'll double check my config. – Chad Jan 06 '14 at 16:37