1

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.

Here is the whole error message, unfortunately i didn't get the line in code where the error is, even though i used

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!

Dina
  • 61
  • 2
  • 10
  • are you using and or setting any `Session[]` variables..? if you are ..then I would recommend 2 things.. `1.` Initialize any and all session variables in the `Globala.asax` file and `2` use `HttpContext.Current.Session` instead of `Session` if this is not the issue please show the relevant code where this error is being thrown.. if you are not sure.. then use the `Debugger` to determine where it's happening – MethodMan Feb 11 '15 at 14:55
  • can you post the code where the exception thrown, looking at the stack trace its in `Page_Load` of `SiteMaster`. it could be that you a referencing something in the request that might not be set from chrome – Dave Becker Feb 11 '15 at 15:07
  • Thank You for Your answer! I edited my question and added the code from the global.asax and page master. Could You please look at it again? Unfortunately i don't have this issue(most users didn't report this issue), the application starts normally for me in the chrome browser, so i can't debug it. – Dina Feb 11 '15 at 15:16
  • possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – mason Feb 11 '15 at 15:29

1 Answers1

1

We don't really have enough information to tell you exactly what went wrong, all I can tell from the trace you've included is that the exception is being thrown in your master page's Page_Load method.

Take a look through this method and check for any point where you access properties of an object without checking that the object exists. Common culprits in my experience are pulling things out of the session or cookies collection and trying to use them without checking that you didn't just pull a null value out. The best way to fix it would be if you could replicate this problem yourself by using chrome and stepping through the Page_Load method.

EDIT: After seeing your posted source, I don't see anything that immediately stands out as breaking chrome compatibility, you have some weird cookie logic imo but nothing breaking. My only suggestion would be for you to try to replicate this further, and add more logging into your solution so that when your users hit this exception you can get more information.

One technique I've used in the past was to deploy the site in debug mode with all the .pdb files in place, and use something like this:

var stack = new StackTrace(exception, true);
// Get the top stack frame
var frame = st.GetFrame(0);
// Get the line number from the stack frame
var line = frame.GetFileLineNumber();

then log that value. It should give you the line number that the exception was thrown on, although as I said nothing in your code stands out as being wrong to me, so I'm not totally sure.

hcp
  • 486
  • 1
  • 3
  • 11
  • Thank You for Your answer! I edited my question and added the code from the global.asax and page master. Could You please look at it again? Unfortunately i don't have this issue, the application starts normally for me in the chrome browser, so i can't debug it. – Dina Feb 11 '15 at 15:15