0

After moving my application to production, I get the regular Object reference not set to an instance of an object. exception logged in my event viewer.

This exception occurs intermittently few times a day, the problem is that I cannot put my hand on the problem, the logged exception details are not helpful, as there are no code line numbers included in the stack trace.

The Exception occurs on an action in my home controller which is the entry point action for each user arriving at the web site.

Can anyone help me reason with the below trace:

ERROR =>  Object reference not set to an instance of an object. 
(XXX.XXX.Web.Controllers.HomeController.Index) 

(
<>c__DisplayClass2b.<BeginInvokeAction>b__1c => 

<>c__DisplayClass33.<BeginInvokeActionMethodWithFilters>b__32 => 

<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f =>

 AsyncInvocationWithFilters.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3d =>

 WrappedAsyncResult`2.CallEndDelegate =>

 AsyncControllerActionInvoker.<BeginInvokeSynchronousActionMethod>b__39 => 

 ControllerActionInvoker.InvokeActionMethod => 

 ReflectedActionDescriptor.Execute => 
 <no type>.lambda_method => HomeController.Index ) 

System.NullReferenceException: Object reference not set to an instance of an object.
   at XXXX.XXXX.Web.Controllers.HomeController.Index()

Code:

[SSOAuthorize]
    public ActionResult Index()
    {
        try
        {

            ViewBag.ShowXXXAccountPrompt = false;
            ViewBag.IsErrored = "false";


            #region "Session Check"
            if (_webHttpContext == null)
            {
                return RedirectToAction("signout", "Federation");
            }
            if (_webHttpContext.Session == null)
            {
                return RedirectToAction("signout", "Federation");
            }
            if (_webHttpContext.Session.PortalUser == null)
            {
                return RedirectToAction("signout", "Federation");
            }
            #endregion 

            if (_webHttpContext.Session.PortalUser != null)
            {
                if (!string.IsNullOrEmpty(_webHttpContext.Session.PortalUser.UserName))
                {
                    var user = _usersManagementService.GetUserByUsername(_webHttpContext.Session.PortalUser.UserName);
                    var latestsubline = _subscriptionLineService.GetLatestLineByUserName(_webHttpContext.Session.PortalUser.UserName);
                    var isDualAccount = latestsubline == null ? false : latestsubline.Action == (int)Entities.SubscriptionLineAction.Activate? latestsubline.IsDualAccount : false;
                    isDualAccount = isDualAccount == null ? false : isDualAccount;
                    if(user == null)
                    {
                        //whenever there is no account Just Create it, the subscriptionLines table is irrelevant
                        _usersManagementService.CreateUser(_webHttpContext.Session.PortalUser.UserName, isDualAccount);
                    }
                    if (user.Status == AccountStatus.Inactive)
                    {
                        //Account Re-Activation Logic
                        if (_usersManagementService.CreateAccountHistory(user))
                         {
                             user.FirstLogonOn = DateTime.Now;
                             user.DeactivatedOn = null;
                             user.Status = AccountStatus.Active;
                             user.IsDualAccount = isDualAccount;
                             _usersManagementService.UpdateAccount(user);

                             #region "Check XXX Dual Account"
                             try
                             {
                                 if (_webHttpContext.Session.MultipleAccountDialogueInformed == false)
                                 {
                                     _webHttpContext.Session.MultipleAccountDialogueInformed = true;

                                     if (user.IsDualAccount == true)
                                     {

                                         ViewBag.ShowXXXAccountPrompt = true;

                                     }
                                     else
                                     {
                                         ViewBag.ShowXXXAccountPrompt = false;
                                     }
                                 }
                             }
                             catch (Exception ex)
                             {
                                 _logger.Log(LogLevel.Error, "XXX Account Service _ " + ex.Message, ex);
                             }
                             #endregion
                         }
                        else
                        {
                            ViewBag.IsErrored = "true";
                            return View(_webHttpContext.Account);
                        }
                    }
                    else
                    {

                        #region "Check XXX Dual Account"
                        try
                        {
                            if (_webHttpContext.Session.MultipleAccountDialogueInformed == false)
                            {
                                _webHttpContext.Session.MultipleAccountDialogueInformed = true;

                                        if (user.IsDualAccount == true)
                                        {

                                            ViewBag.ShowXXXAccountPrompt = true;

                                        }
                                        else
                                        {
                                            ViewBag.ShowXXXAccountPrompt = false;
                                        }
                                }
                         }
                        catch (Exception ex)
                        {
                            _logger.Log(LogLevel.Error, "XXX Account Service _ " + ex.Message, ex);
                        }
                        #endregion
                    }
                    _webHttpContext.BrowserDetails.OperatingSystem = Entities.OperatingSystem.MacinXXXh;

                    _webHttpContext.Session.PortalUser.Id = user.Id;
                    if (_webHttpContext.BrowserDetails.OperatingSystem == Entities.OperatingSystem.Windows)
                    {
                        ViewBag.ProductType = "ZZZ";
                        var YYYInstallsDetails = GetYYYInstallsDetails(0);
                        ViewBag.RemainingInstallCount = YYYInstallsDetails.NumberOfSeatsAvailable;
                        ViewBag.YYYInstallsDetails = YYYInstallsDetails.YYYCustomersLicenseDetailsList;
                        ViewBag.YYYInstallsDetailsCount = YYYInstallsDetails.YYYCustomersLicenseDetailsList.Count;
                        ViewBag.DownloadPageUrl = getDownloadPageUrl(ProductType.ComputerProtectionNT);
                        ViewBag.ProductId = (int)ProductType.ComputerProtectionNT;
                    }
                    else if (_webHttpContext.BrowserDetails.OperatingSystem == Entities.OperatingSystem.MacinXXXh)
                    {
                        ViewBag.ProductType = "MacZZZ";
                        var YYYInstallsDetails = GetYYYInstallsDetails(1);
                        ViewBag.RemainingInstallCount = YYYInstallsDetails.NumberOfSeatsAvailable;
                        ViewBag.YYYInstallsDetails = YYYInstallsDetails.YYYCustomersLicenseDetailsList;
                        ViewBag.YYYInstallsDetailsCount = YYYInstallsDetails.YYYCustomersLicenseDetailsList.Count;
                        ViewBag.DownloadPageUrl = getDownloadPageUrl(ProductType.ComputerProtectionOSX);
                        ViewBag.ProductId = (int)ProductType.ComputerProtectionOSX;
                    }
                    else if (_webHttpContext.BrowserDetails.OperatingSystem == Entities.OperatingSystem.Other)
                    {
                        ViewBag.ProductType = "AndroidZZZ";
                        var YYYInstallsDetails = GetYYYInstallsDetails(2);
                        ViewBag.RemainingInstallCount = YYYInstallsDetails.NumberOfSeatsAvailable;
                        ViewBag.YYYInstallsDetails = YYYInstallsDetails.YYYCustomersLicenseDetailsList;
                        ViewBag.YYYInstallsDetailsCount = YYYInstallsDetails.YYYCustomersLicenseDetailsList.Count;
                        ViewBag.DownloadPageUrl = getDownloadPageUrl(ProductType.ComputerProtectionAndroid);
                        ViewBag.ProductId = (int)ProductType.ComputerProtectionAndroid;
                    }
                    ViewBag.ZZZInstallUrl = PortalConfig.ZZZInstallUrl;
                    ViewBag.ScInstallUrl = PortalConfig.SafeCentralInstallUrl;
                    ViewBag.MacZZZInstallUrl = PortalConfig.MacZZZInstallUrl;
                    ViewBag.TTTTEmail = _webHttpContext.Session.PortalUser.UserName;

                }
                else // if primary username coming back from sso is empty
                {

                    return RedirectToAction("signout", "Federation");
                }
            }
            else // if the portal user is null 
            {


                return RedirectToAction("signout", "Federation");
            }

        }
        catch (Exception ex)
        {
            ViewBag.IsErrored = "true";
            _logger.Log(LogLevel.Error, ex.Message, ex);

        }
        return View(_webHttpContext.Account);
    }
Basem Sayej
  • 3,374
  • 1
  • 14
  • 12
  • 2
    Adding that action code would help *a lot* – Andre Calil Aug 07 '15 at 03:19
  • You should start with [What is a NullReferenceException and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) and then elaborate if that doesn't help you. – Rowan Freeman Aug 07 '15 at 03:31
  • I know what a null reference exception is, i dont know which object is causing it, especially that i do all kinds of null reference checking before calling any method or attribute on any object. i cant share the code due to legalities – Basem Sayej Aug 07 '15 at 04:02
  • and im pretty sure that if an object is null referenced inside this action the stack trace would include the line number where the exception occurred, thats why i'm suspecting that the exception is raised before the execution reaching the first statement in the action. – Basem Sayej Aug 07 '15 at 04:11
  • what parameter/s does your HomeController/Index have? – Cacho Santa Aug 07 '15 at 04:13
  • You're not expected to post an entire solution, but if you want help, you need to post enough code to reproduce the problem. That being said, [ELMAH](https://www.nuget.org/packages/Elmah.MVC/) can usually log these errors, depending on where in the pipeline the exception originates. – Tieson T. Aug 07 '15 at 04:13
  • it doesnt take any parameters. – Basem Sayej Aug 07 '15 at 04:14
  • I have posted the code and masked some values in it. knowing that this action method is accessed daily by 2000+ users, where the exception occurs on average around 10 times a day – Basem Sayej Aug 07 '15 at 04:21
  • I'm assuming anything starting with an underscore is a class-level variable - how are those managed? Are you using a DI framework to inject them into the controller? If I had to guess, it seems like one of your services might have been disposed, since that's the first place it looks like you're not checking for nulls. There's also your custom attribute - I assume that mostly just wraps the AuthorizeAttribute? – Tieson T. Aug 07 '15 at 04:43
  • yes all services objects(that start with an underscore) are constructor injected thats why i never check for their nullability, as they're always constructed in the constructor through unity. thus im excluding the possibility of them being null, especially that this exception is raised intermittently less than 1% of the time. and you are correct in regards the authorize attribute. – Basem Sayej Aug 07 '15 at 04:52
  • Depending on how you've configured your container, it's entirely possible for injected objects to become null, if you've set the lifetime incorrectly. Does your custom attribute attempt to access Session at any point? An unhandled exception crashes the AppPool, which in turn causes Session to become null. – Tieson T. Aug 08 '15 at 22:07

0 Answers0