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:
Object reference not set to an instance of an object.
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.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
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.
Stack Trace:
[NullReferenceException: Object reference not set to an instance of an object.]
AI.Controllers.HomeController.Index() +174
lambda_method(Closure , ControllerBase , Object[] ) +86
System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary2 parameters) +228 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary
2 parameters) +34
System.Web.Mvc.Async.AsyncControllerActionInvoker.b__39(IAsyncResult asyncResult, ActionInvocation innerInvokeState) +38
System.Web.Mvc.Async.WrappedAsyncResult2.CallEndDelegate(IAsyncResult asyncResult) +69 System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) +41 System.Web.Mvc.Async.AsyncInvocationWithFilters.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3d() +71 System.Web.Mvc.Async.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f() +387 System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult) +42 System.Web.Mvc.Async.<>c__DisplayClass2b.<BeginInvokeAction>b__1c() +38 System.Web.Mvc.Async.<>c__DisplayClass21.<BeginInvokeAction>b__1e(IAsyncResult asyncResult) +188 System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +38 System.Web.Mvc.Controller.<BeginExecuteCore>b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +29 System.Web.Mvc.Async.WrappedAsyncVoid
1.CallEndDelegate(IAsyncResult asyncResult) +68
System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +52
System.Web.Mvc.Async.WrappedAsyncVoid1.CallEndDelegate(IAsyncResult asyncResult) +39 System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +38 System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__5(IAsyncResult asyncResult, ProcessRequestState innerState) +43 System.Web.Mvc.Async.WrappedAsyncVoid
1.CallEndDelegate(IAsyncResult asyncResult) +68
System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +38
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +602
System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep step) +195
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +128
Version Information:
- Microsoft .NET Framework Version:4.0.30319;
- ASP.NET Version:4.7.3770.0
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 "+174" is supposed to be a line number in the source code, that's the last line in the file.
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.
Here is the complete code in my HomeController.cs file:
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("~/Views/Shared/PageNotFound.cshtml");
}
public ActionResult Index()
{
//do initial routing and authorization
bool thereAreClaims = false;
UserContext uc = new UserContext();
User thisUser = (User)Session["userInfo"];
if (thisUser.ACCESS_ID == "") { return RedirectToRoute(new { controller = "Home", action = "NotAuthorized" }); }
//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 = "Admin", action = "Claims" }); // 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 = "Home", action = "GettingStarted" }); // choice page
}
else
{
return RedirectToRoute(new { controller = "Educator", action = "GettingStarted" }); // need to get action_security when we get there.
}
}
else
{
if (thereAreClaims)
{
return RedirectToRoute(new { controller = "Student", action = "GettingStarted" }); // need to get action_security when we get there.
}
else
{
return RedirectToRoute(new { controller = "Home", action = "NoClaimsStudent" });
}
}
}
else
{
if (thisUser.ut.IS_EDUCATOR)
{
return RedirectToRoute(new { controller = "Educator", action = "GettingStarted" }); // need to get action_security when we get there.
}
else
{
return RedirectToRoute(new { controller = "Home", action = "NotAuthorized" });
}
}
}
}
// GET: /Home/NotAuthorized
public ActionResult NotAuthorized()
{
return View("~/Views/Shared/NoAuth.cshtml");
}
public ActionResult NotAuthorizedStudent()
{
return View("~/Views/Shared/NoAuthStudent.cshtml");
}
public ActionResult NotAuthorizedEducator()
{
return View("~/Views/Shared/NoAuthEducator.cshtml");
}
public ActionResult NotAuthorizedToDownload()
{
return View("~/Views/Shared/NoAuthViewDoc.cshtml");
}
public ActionResult FileNotFound()
{
return View("~/Views/Shared/NoFile.cshtml");
}
public ActionResult NoClaimsStudent() // but this one *should* have the photo
{
return View("~/Views/Shared/NoClaimsStudent.cshtml");
}
//[AuthorizeView]
public ActionResult GettingStarted()
{
return View("~/Views/Shared/Choice.cshtml");
}
public ActionResult PageNotFound()
{
return View("~/Views/Shared/PageNotFound.cshtml");
}
public ActionResult UnMask()
{
string action_data = "Home/UnMask";
GlobalConfigContext gcc = new GlobalConfigContext();
GlobalConfig gc = gcc.getGlobalConfig();
if ((bool)Session["impersonate"] && (string)Session["orig_access_id"] != "" )
{
UserContext uc = new UserContext();
User unMasked = uc.getUserInfo((string)Session["orig_access_id"]);
if (unMasked.CAN_IMPERSONATE)
{
Session["impersonate"] = false;
Session["orig_access_id"] = "";
Session["userInfo"] = (User)unMasked;
Session["username"] = (string)unMasked.ACCESS_ID;
}
else
{
LoggingContext.Log("Unauthorized access to page", action_data, unMasked.ACCESS_ID);
}
}
else
{
LoggingContext.Log("Invalid attempt to impersonate", action_data, (string)Session["orig_access_id"]);
}
return RedirectToRoute(new { controller = "Home", action = "Index" });
}
public void logtest ()
{
LoggingContext.Log("Logtest", "Home/Logtest", (string)Session["username"]);
}
}
}
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
BTW: I looked at What is a NullReferenceException, and how do I fix it? but the problem is that I am unable to debug after deployment and in debug mode it works fine