title: Why does session object throw a null reference exception?
body: <p>on some of my aspx page I am checking session like this</p>
  
  <pre><code>if (bool.Parse(Session["YourAssessment"].ToString()) == false
      &amp;&amp; bool.Parse(Session["MyAssessment"].ToString()) == true)
  {
      Response.Redirect("~/myAssessment.aspx");
  }
  </code></pre>
  
  <p>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 </p>
  
  <pre><code>Object reference not set to an instance of an object.
  </code></pre>
  
  <p>Following is the stack for this</p>
  
  <pre><code>[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
  </code></pre>
  
  <p>Could some body explain me this weird behaviour?</p>
  
  <p>And as we know by default the session last for is 20 min.</p>
  
  <p>EDITED</p>
  
  <p>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</p>
  
  <pre><code>protected void Page_Load(object sender, EventArgs e)
  {
      if (!HttpContext.Current.Request.IsAuthenticated)
      {
          Response.Redirect("~/login.aspx");
      }
      else
      {
          Session["YourAssessment"] = false;
          Session["MyAssessment"] = false;
      }
  }
  </code></pre>
  
  <p>on button click it has</p>
  
  <pre><code>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 &gt; 0)
          {
              Session["YourAssessment"] = true;
              Session["MyAssessment"] = false;
          }
          Response.Redirect((countSurvey &gt; 0)
              ? "~/yourAssessment.aspx"
              : "~/myAssessment.aspx");
      }
      else
      {
          Response.Redirect("~/login.aspx");
      }
  </code></pre>
  
  <p>and on myAssessment page it check like this</p>
  
  <pre><code>protected void Page_Load(object sender, EventArgs e)
  {
      if (!HttpContext.Current.Request.IsAuthenticated)
      {
          Response.Redirect("~/login.aspx");
      }
      else
      {
          if (Session["YourAssessment"] != null
              &amp;&amp; Session["MyAssessment"] != null
              &amp;&amp; bool.Parse(Session["YourAssessment"].ToString())
              &amp;&amp; !bool.Parse(Session["myAssessment"].ToString()))
          {
              Response.Redirect("~/yourAssessment.aspx");
          }
      }
  }
  </code></pre>
  
  <p>and on yourAssessmtn it check like this</p>
  
  <pre><code>protected void Page_Load(object sender, EventArgs e)
  {
      if (!HttpContext.Current.Request.IsAuthenticated)
      {
          Response.Redirect("~/login.aspx");
      }
      else
      {
          if (Session["YourAssessment"] != null
              &amp;&amp; Session["MyAssessment"] != null
              &amp;&amp; !bool.Parse(Session["YourAssessment"].ToString())
              &amp;&amp; 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;
          }
      }
  }
  </code></pre>
  
  <p>what's wrong please explain?</p>