I made a web application, and tested it locally. Everything worked fine so it was deployed to the hosting server. After a few weeks some of the users started to experience the "Object reference not set to an instance of an object" error. This is currently happening only in the chrome browser(on other browsers the application starts normally for them), and also if they use the incognito mode in chrome the application is opening normally.
I can't debug the problem because the application is working fine for me(and most other users) in chrome.
Here is the whole error message, unfortunately i didn't get the line in code where the error is, even though i used <customerrors mode="Off"> in the web.config
Server Error in '/' Application.
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.]
INAWebAppTest.SiteMaster.Page_Load(Object sender, EventArgs e) +884
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +51
System.Web.UI.Control.OnLoad(EventArgs e) +92
System.Web.UI.Control.LoadRecursive() +54
System.Web.UI.Control.LoadRecursive() +145
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +772
I don't know if this can be because of a newer version of application being deployed on the server, in combination with the users cookies?
If so, can You please advise what i could try to correct this issue. I can't ask all of the users to clean up their browser history and cookies, there are too many of them, and it wouldn't solve this issue if it happened again in the future.
Here is the code from the site master:
public partial class SiteLogin : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Request.Cookies["userInfo"] != null)
{
Response.Cookies.Set(Request.Cookies["userInfo"]);
Response.Cookies["userInfo"]["lastVisit"] = DateTime.Now.ToString();
Response.Cookies["userInfo"].Expires = DateTime.Now.AddDays(7);
if (Server.HtmlEncode(Request.Cookies["userInfo"]["userName"]) == null)
{
Response.Redirect("~/Account/login.aspx");
}
if (Server.HtmlEncode(Request.Cookies["userInfo"]["role"]).Equals("3"))
Response.Redirect("~/Form/Progress.aspx");
if (Server.HtmlEncode(Request.Cookies["userInfo"]["role"]).Equals("2"))
Response.Redirect("~/CForm/CHome.aspx");
if (Server.HtmlEncode(Request.Cookies["userInfo"]["role"]).Equals("1"))
Response.Redirect("~/AForm/AHome.aspx");
}
}
}
}
Here is the code from global.asax page:
protected void Session_Start(object sender, EventArgs e)
{
string sessionId;
if (Session.SessionID!=null)
sessionId = Session.SessionID;
else
{
SmtpClient client = new SmtpClient();
client.Port = xxx;
client.Host = "xxx.xxx.xxx.xxx";
client.EnableSsl = false;
client.Timeout = 10000;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential("");
MailMessage mm = new MailMessage();
mm.From = new MailAddress("xxx", "xxx");
mm.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
mm.Subject = "Null object";
mm.IsBodyHtml = true;
mm.To.Add("xxx");
string body = "null";
mm.Body = body;
try
{
client.Send(mm);
}
catch
{
}
}
In the first version of the application the global.asax was empty, but there were some problems with session being flushed, so I googled and found that the solution is this line of code:
string sessionId = Session.SessionID;
This fixed that issue, but since some users were having the problem mentioned in this subject, i added some code around so that i get a message if the session variable isn't found. I never received this mail, so i don't think the problem is there.
Thank You for the help!