简体   繁体   中英

Why does session object throw a null reference exception?

on some of my aspx page I am checking session like this

if (bool.Parse(Session["YourAssessment"].ToString()) == false
    && bool.Parse(Session["MyAssessment"].ToString()) == true)
{
    Response.Redirect("~/myAssessment.aspx");
}

It works fine if I keep playing with the pages frequently, but if I don't do anything with the page at least even for 5 min, running the page throws the error

Object reference not set to an instance of an object.

Following is the stack for this

[NullReferenceException: Object reference not set to an instance of an object.]
   yourAssessment.Page_Load(Object sender, EventArgs e) in d:\Projects\NexLev\yourAssessment.aspx.cs:27
   System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14
   System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35
   System.Web.UI.Control.OnLoad(EventArgs e) +91
   System.Web.UI.Control.LoadRecursive() +74
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2207

Could some body explain me this weird behaviour?

And as we know by default the session last for is 20 min.

EDITED

See I have a page default aspx, it has got a button which fixes on the some basis where to redirect On default page it check like this

protected void Page_Load(object sender, EventArgs e)
{
    if (!HttpContext.Current.Request.IsAuthenticated)
    {
        Response.Redirect("~/login.aspx");
    }
    else
    {
        Session["YourAssessment"] = false;
        Session["MyAssessment"] = false;
    }
}

on button click it has

protected void imgClientFreeEval_Click(object sender,
    System.Web.UI.ImageClickEventArgs e)
{
    if (HttpContext.Current.Request.IsAuthenticated)
    {
        string sqlQuery = "SELECT count(*) FROM SurveyClient WHERE UserID='"
            + cWebUtil.GetCurrentUserID().ToString() + "'";
        SqlParameter[] arrParams = new SqlParameter[0];
        int countSurvey = int.Parse(
            Data.GetSQLScalerVarQueryResults(sqlQuery).ToString());
        if (countSurvey > 0)
        {
            Session["YourAssessment"] = true;
            Session["MyAssessment"] = false;
        }
        Response.Redirect((countSurvey > 0)
            ? "~/yourAssessment.aspx"
            : "~/myAssessment.aspx");
    }
    else
    {
        Response.Redirect("~/login.aspx");
    }

and on myAssessment page it check like this

protected void Page_Load(object sender, EventArgs e)
{
    if (!HttpContext.Current.Request.IsAuthenticated)
    {
        Response.Redirect("~/login.aspx");
    }
    else
    {
        if (Session["YourAssessment"] != null
            && Session["MyAssessment"] != null
            && bool.Parse(Session["YourAssessment"].ToString())
            && !bool.Parse(Session["myAssessment"].ToString()))
        {
            Response.Redirect("~/yourAssessment.aspx");
        }
    }
}

and on yourAssessmtn it check like this

protected void Page_Load(object sender, EventArgs e)
{
    if (!HttpContext.Current.Request.IsAuthenticated)
    {
        Response.Redirect("~/login.aspx");
    }
    else
    {
        if (Session["YourAssessment"] != null
            && Session["MyAssessment"] != null
            && !bool.Parse(Session["YourAssessment"].ToString())
            && bool.Parse(Session["MyAssessment"].ToString()))
        {
            Response.Redirect("~/myAssessment.aspx");
        }

        PopulateAllSurveyByUser();
        if (ViewState["surveyClientID"] != null)
        {
            grdSurveyDetail.Visible = true;
            PopulateSurveyDetails(
                int.Parse(ViewState["surveyClientID"].ToString()));
        }
        else
        {
            grdSurveyDetail.Visible = false;
        }
    }
}

what's wrong please explain?

You first need to check whether that session variable exists

if(Session["YourAssessment"] != null)
    // Do something with it
else
    // trying to call Session["YourAssessment"].ToString() here will FAIL.

That happens since your session has a lifecycle, which means - it expires (the cookie that defines it expires) - thus your objects vanish. you could increase sessionState timeout in web.config for your sessions to last longer.

For example, in web.config

  <system.web>
      <sessionState timeout="40" />
  </system.web>

Will make your sessions last for 40 minutes, as long as the client doesn't clear it, and the web server is up&running.

Always check for null when accessing Session object!
You can write some small utility that can be used for that:

public class SessionData
{
    public static T Get<T>(string key)
    {
        object value = HttpContext.Current.Session[key];

        if(value == null)
            return default(T);

        try
        {
            return (T)value;
        }
        catch(Exception e)
        {
            return default(T);
        }
    }

    public static void Put(string key, object value)
    {
        HttpContext.Current.Session[key] = value;
    }
}

Session can be null if app pool is recycled. That can happen because numerous reasons...

One trick for keeping your server from loosing session is making "pings" to server from javascript. It can make requests to some dummy url (empty page, or if you're perf freak, to .ashx handler) every minute or so. It can be useful for pages that you keep open for long time, like huge edit forms.
Also, beware, there are different session timeout values for debug and release configuration!

First you can use you code like this

if (!bool.Parse(Session["YourAssessment"].ToString()) && 
     bool.Parse(Session["MyAssessment"].ToString()))
    Response.Redirect("~/myAssessment.aspx");

You sure you Sessions are not null

Check like this

if (Session["YourAssessment"] != null && Session["MyAssessment"] != null && 
    !bool.Parse(Session["YourAssessment"].ToString()) && 
     bool.Parse(Session["MyAssessment"].ToString()))
        Response.Redirect("~/myAssessment.aspx");

如果Session不为null,请重新检查它是否具有"YourAssessment""MyAssessment"

When your Session expires the objects you have placed in your session such as Session["YourAssessment"] become null and the .toString() method call on those objects will then throw an Object reference error. To work around this you must first check to make sure your session variables being null before attempting to perform the toString().

    if(Session["YourAssessment"] != null){
if (bool.Parse(Session["YourAssessment"].ToString()) == false &&    bool.Parse(Session["MyAssessment"].ToString()) == true)
        {
            Response.Redirect("~/myAssessment.aspx");
        }
    }

Instead of the .ToString and Boolean.Parse do Convert.ToBoolean(Session["YourAssessment"])

When I try Boolean b = Convert.ToBoolean(null) b = false ;)

Well after all that have been written about this it seems that problem is IIS application restarting and if your session is stored inproc that can couse deletion of session variables. So try to log application end events and see if this is a case, put this in Global.asax.cs application_end event, this code would log application restart and why it happened :

protected void Application_End(object sender, EventArgs e)
{
  HttpRuntime runtime = (HttpRuntime)typeof(System.Web.HttpRuntime).InvokeMember("_theRuntime", BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.GetField, null, null, null);

  string shutDownMessage = "";

  if (runtime != null)
  {
    shutDownMessage = Environment.NewLine + "Shutdown: " +
                      (string)runtime.GetType().InvokeMember("_shutDownMessage", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.GetField, null, runtime, null) + 
                      Environment.NewLine + "Stack: " + Environment.NewLine +
                      (string)runtime.GetType().InvokeMember("_shutDownStack", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.GetField, null, runtime, null);
  }

  string logFile =  HttpContext.Current.Server.MapPath(~/AppEndLog.log");
  string logMsg = "";
  if (File.Exists(logFile))
    logMsg = logMsg + File.ReadAllText(logFile) + Environment.NewLine + Environment.NewLine;
  logMsg = logMsg + Environment.NewLine + "ApplicationEnd - " + DateTime.Now.ToString() + shutDownMessage;
  File.WriteAllText(logFile, logMsg);


}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM