0

I am getting a Null Reference exception on the following line:

private void BindData()
{
         string username = Session["Username"].ToString();

This error is occurring when a user is logged in, stays idle for a few minutes and refreshed the page. I had the following in my Web.Config which I though might have caused the error but when I commented it, the error still occurred:

<sessionState timeout="20" />

Stack Trace:

    [NullReferenceException: Object reference not set to an instance of an object.]
   _1.PresentationLayer.WebPages.ViewItFaults.BindData() in C:\Users\Rcc\Desktop\Development\Ticketing system\1.PresentationLayer\WebPages\ViewItFaults.aspx.cs:26
   _1.PresentationLayer.WebPages.ViewItFaults.Page_Load(Object sender, EventArgs e) in C:\Users\Rcc\Desktop\Development\Ticketing system\1.PresentationLayer\WebPages\ViewItFaults.aspx.cs:19
   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.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +772
rikket
  • 2,357
  • 7
  • 46
  • 74
  • How have you confirmed that there is a value with that key in the session? – Richard Nov 18 '14 at 13:32
  • @Richard, yes there is a value which is set when the user loggs in. The page works, the error only occurs after being idle for a few minutes – rikket Nov 18 '14 at 13:36
  • But how are you confirming this (just because it is set does not mean something else, including session timeout, cannot remove it)? – Richard Nov 18 '14 at 13:37
  • Almost all cases of `NullReferenceException` are the same. Please see "[What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)" for some hints. – John Saunders Nov 18 '14 at 13:50

1 Answers1

0

When you comment out that line, ASP.net uses the default timeout, which is 20. So, commenting that line doesn't change anything. If you want a session to last longer, specify a higher timeout. 525600 minutes is the longest possible timeout (1 year)

<sessionState timeout="525600" />

On a side note, make sure your exception is handled correctly, avoid yellow screens of death at all costs

cpacheco
  • 186
  • 9