title: Object reference not set to an instance of an object when deploying ASP.NET 4.5 MVC
body: <p>I have an ASP.NET 4.5 MVC application written in C#. I'm using VS 2019. I have successfully deployed this project in the past, but this time, after I publish and move the code to the server, I am getting the following error:</p>
  <blockquote>
  <p>Object reference not set to an instance of an object.</p>
  <p>Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.</p>
  <p>Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.</p>
  <p>Source Error:</p>
  <p>An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.</p>
  <p>Stack Trace:</p>
  <p>[NullReferenceException: Object reference not set to an instance of an object.]<br />
  AI.Controllers.HomeController.Index() +174<br />
  lambda_method(Closure , ControllerBase , Object[] ) +86<br />
  System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary<code>2 parameters) +228   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary</code>2 parameters) +34<br />
  System.Web.Mvc.Async.AsyncControllerActionInvoker.b__39(IAsyncResult asyncResult, ActionInvocation innerInvokeState) +38<br />
  System.Web.Mvc.Async.WrappedAsyncResult<code>2.CallEndDelegate(IAsyncResult asyncResult) +69   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) +41   System.Web.Mvc.Async.AsyncInvocationWithFilters.&lt;InvokeActionMethodFilterAsynchronouslyRecursive&gt;b__3d() +71   System.Web.Mvc.Async.&lt;&gt;c__DisplayClass46.&lt;InvokeActionMethodFilterAsynchronouslyRecursive&gt;b__3f() +387   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult) +42   System.Web.Mvc.Async.&lt;&gt;c__DisplayClass2b.&lt;BeginInvokeAction&gt;b__1c() +38   System.Web.Mvc.Async.&lt;&gt;c__DisplayClass21.&lt;BeginInvokeAction&gt;b__1e(IAsyncResult asyncResult) +188   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +38   System.Web.Mvc.Controller.&lt;BeginExecuteCore&gt;b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +29   System.Web.Mvc.Async.WrappedAsyncVoid</code>1.CallEndDelegate(IAsyncResult asyncResult) +68<br />
  System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +52<br />
  System.Web.Mvc.Async.WrappedAsyncVoid<code>1.CallEndDelegate(IAsyncResult asyncResult) +39   System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +38   System.Web.Mvc.MvcHandler.&lt;BeginProcessRequest&gt;b__5(IAsyncResult asyncResult, ProcessRequestState innerState) +43   System.Web.Mvc.Async.WrappedAsyncVoid</code>1.CallEndDelegate(IAsyncResult asyncResult) +68<br />
  System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +38<br />
  System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +602<br />
  System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep step) +195<br />
  System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean&amp; completedSynchronously) +128</p>
  </blockquote>
  <p>Version Information:</p>
  <ul>
  <li>Microsoft .NET Framework Version:4.0.30319;</li>
  <li>ASP.NET Version:4.7.3770.0</li>
  </ul>
  <p>The application works fine when I run it in debug mode in VS2019; I only get this error after I deploy. I can't set any breakpoints or find out what might be wrong in the deployed code, and the stack trace doesn't tell me where my error is either -- if the &quot;+174&quot; is supposed to be a line number in the source code, that's the last line in the file.</p>
  <p>The second line in the stack trace is lambda_method(Closure , ControllerBase , Object[] ) +86 but there are no lambda expressions in the c# of the HomeController.sc file, so that seems to be a red herring as well.</p>
  <p>Here is the complete code in my HomeController.cs file:</p>
  <pre><code>using System.Reflection;
  using System.Text;
  using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.Web.Mvc;
  using AI.auth;
  using AI.util;
  using AI.Models;
  
  namespace AI.Controllers
  {
      public class HomeController : Controller
      {
          //
          // GET: /Home/
          public class AjaxRequestAttribute : ActionMethodSelectorAttribute
          {
              public override bool IsValidForRequest(ControllerContext controllerContext, MethodInfo methodInfo)
              {
                  return controllerContext.HttpContext.Request.IsAjaxRequest();
              }
          }
  
          protected override JsonResult Json(object data, string contentType, Encoding contentEncoding, JsonRequestBehavior behavior)
          {
              return new JsonNetResult
              {
                  Data = data,
                  ContentType = contentType,
                  ContentEncoding = contentEncoding,
                  JsonRequestBehavior = behavior
              };
          }
  
          public ActionResult TestBlank() // can be removed after testing
          {
              return View(&quot;~/Views/Shared/PageNotFound.cshtml&quot;);
          }
  
          public ActionResult Index()
          {
              //do initial routing and authorization
              bool thereAreClaims = false;
  
              UserContext uc = new UserContext();
  
              User thisUser = (User)Session[&quot;userInfo&quot;];
              if (thisUser.ACCESS_ID == &quot;&quot;) { return RedirectToRoute(new { controller = &quot;Home&quot;, action = &quot;NotAuthorized&quot; }); }
  
              //if (thisUser.ut.IS_STUDENT)
              //{
              thereAreClaims = StudentContext.checkClaims(thisUser.ACCESS_ID);
              //}
              if (thereAreClaims) { thisUser.ut.IS_STUDENT = true; }
  
              if (thisUser.ut.IS_ADMIN)
              {
                  return RedirectToRoute(new { controller = &quot;Admin&quot;, action = &quot;Claims&quot; }); // need to get action_security when we get there.
              }
              else
              {
                  if (thisUser.ut.IS_STUDENT)
                  {
                      if (thisUser.ut.IS_EDUCATOR) // 
                      {
  
                          if (thereAreClaims)
                          {
                              return RedirectToRoute(new { controller = &quot;Home&quot;, action = &quot;GettingStarted&quot; }); // choice page
                          }
                          else
                          {
                              return RedirectToRoute(new { controller = &quot;Educator&quot;, action = &quot;GettingStarted&quot; }); // need to get action_security when we get there.
                          }
                      }
                      else
                      {
                          if (thereAreClaims)
                          {
                              return RedirectToRoute(new { controller = &quot;Student&quot;, action = &quot;GettingStarted&quot; }); // need to get action_security when we get there.
                          }
                          else
                          {
                              return RedirectToRoute(new { controller = &quot;Home&quot;, action = &quot;NoClaimsStudent&quot; });
                          }
                      }
                  }
                  else
                  {
                      if (thisUser.ut.IS_EDUCATOR)
                      {
                          return RedirectToRoute(new { controller = &quot;Educator&quot;, action = &quot;GettingStarted&quot; }); // need to get action_security when we get there.
                      }
                      else
                      {
                          return RedirectToRoute(new { controller = &quot;Home&quot;, action = &quot;NotAuthorized&quot; });
                      }
                  }
              }
          }
  
          // GET: /Home/NotAuthorized
          public ActionResult NotAuthorized()
          {
              return View(&quot;~/Views/Shared/NoAuth.cshtml&quot;);
          }
  
          public ActionResult NotAuthorizedStudent() 
          {
              return View(&quot;~/Views/Shared/NoAuthStudent.cshtml&quot;);
          }
  
          public ActionResult NotAuthorizedEducator() 
          {
              return View(&quot;~/Views/Shared/NoAuthEducator.cshtml&quot;);
          }
  
          public ActionResult NotAuthorizedToDownload()
          {
              return View(&quot;~/Views/Shared/NoAuthViewDoc.cshtml&quot;);
          }
  
          public ActionResult FileNotFound()
          {
              return View(&quot;~/Views/Shared/NoFile.cshtml&quot;);
          }
  
          public ActionResult NoClaimsStudent()  // but this one *should* have the photo
          {
              return View(&quot;~/Views/Shared/NoClaimsStudent.cshtml&quot;);
          }
  
          //[AuthorizeView]
          public ActionResult GettingStarted() 
          {
              return View(&quot;~/Views/Shared/Choice.cshtml&quot;);
          }
  
          public ActionResult PageNotFound()
          {
              return View(&quot;~/Views/Shared/PageNotFound.cshtml&quot;);
          }
  
          public ActionResult UnMask()
          {
              string action_data = &quot;Home/UnMask&quot;;
              GlobalConfigContext gcc = new GlobalConfigContext();
              GlobalConfig gc = gcc.getGlobalConfig();
              if ((bool)Session[&quot;impersonate&quot;] &amp;&amp; (string)Session[&quot;orig_access_id&quot;] != &quot;&quot; )
              {
                  UserContext uc = new UserContext();
                  User unMasked = uc.getUserInfo((string)Session[&quot;orig_access_id&quot;]);
  
                  if (unMasked.CAN_IMPERSONATE)
                  {
                      Session[&quot;impersonate&quot;] = false;
                      Session[&quot;orig_access_id&quot;] = &quot;&quot;;
                      Session[&quot;userInfo&quot;] = (User)unMasked;
                      Session[&quot;username&quot;] = (string)unMasked.ACCESS_ID;
                  }
                  else
                  {
                      LoggingContext.Log(&quot;Unauthorized access to page&quot;, action_data, unMasked.ACCESS_ID);
                  }
              }
              else
              {
                  LoggingContext.Log(&quot;Invalid attempt to impersonate&quot;, action_data, (string)Session[&quot;orig_access_id&quot;]);
              }
  
              return RedirectToRoute(new { controller = &quot;Home&quot;, action = &quot;Index&quot; });
          }
  
          public void logtest ()
          {
               LoggingContext.Log(&quot;Logtest&quot;, &quot;Home/Logtest&quot;, (string)Session[&quot;username&quot;]);
          }
      }
  }
  </code></pre>
  <p>Any suggestions for how to debug this problem? Is it possible that it is a problem with .net versioning? The target version in the VS project is 4.5 so I'm not sure where the Version:4.7.3770.0 is coming from.Thanks for any suggestions</p>
  <p>BTW: I looked at <a href="https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it">What is a NullReferenceException, and how do I fix it?</a> but the problem is that I am unable to debug after deployment and in debug mode it works fine</p>