Solved  "Object reference not set to an instance of an object"

Posted on 2014-10-21
Last Modified: 2014-10-21
I know this is an age old problem, but I am having trouble with some legacy WebForms ASP.Net stuff that I must support.  The code is well written, but the developer never took into account the predictable loss of session state after the default 20 minute timeout.  

I know I can bump the timeout up a bit, but that is not the best solution.  I just want the code to sense an error (globally) and reroute itself to the home page and throw a simple error such as, "Your session timed out".

I have other web sites that do just this. But what do I need to implement to make this happen?  I don't want to go to each page of legacy code to check for a session variable.  

Thanks in advance.
Question by:rowek
  • 4
  • 2
LVL 40

Accepted Solution

Kyle Abrahams earned 500 total points
ID: 40395535
You can redirect to a page using the global.asax:
/// <summary>
    /// The event occurs just after Initialization of Session, and before Page_Init event
    /// </summary>
    protected void Application_PreRequestHandlerExecute(Object sender, EventArgs e)
        // here it checks if session is required, as
        // .aspx requires session, and session should be available there
        // .jpg, or .css does require session so session will be null
        // as .jpg, or .css are also http request in either case if you implemented URL Rewritter, or custom IHttp Module
        if (Context.Handler is IRequiresSessionState || Context.Handler is IReadOnlySessionState)
            // here is your actual code
            // check if session is new one
            // or any of your logic
            if (Session.IsNewSession                || Session.Count < 1)
                // checking if request is not for default.aspx page, as it should not be redirected
                if (!Context.Request.Url.AbsoluteUri.ToLower().Contains("/default.aspx"))

Open in new window


Author Comment

ID: 40395732
Kyle, just want to make sure I understand the code.  There must be an event called Application_PreRequestHandlerExecute.  Your documentation says "The event occurs just after Initialization of Session".  Is this a sure fire way to capture the timeout?  I will try to implement.  

It should not matter that the legacy app (ASP.NET 2.0) is written in VB.NET, does it?  It has been upgraded to .NET 4.0.  We had to stick with 4.0 because it runs on a 2003 Server.

Author Comment

ID: 40395754
Kyle, the event never fires.  The solution seems very elegant, but I don't get my event.  Here is my converted code:

    Sub Application_PreRequestHandlerExecutive(ByVal sender As Object, ByVal e As EventArgs)
        ' Code that runs just after Initialization of Session and before Page_Init event
        If TypeOf Context.Handler Is IRequiresSessionState OrElse TypeOf Context.Handler Is IReadOnlySessionState Then
            ' check if session is new one
            ' or any of your logic
            If Session.IsNewSession OrElse Session.Count < 1 Then
                ' checking if request is not for default.aspx page, as it should not be redirected
                If Not Context.Request.Url.AbsoluteUri.ToLower().Contains("/default.aspx") Then
                End If
            End If
        End If
    End Sub
Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.


Author Comment

ID: 40395758
Kyle, you are gonna LOVE this discovery.  My C# to converter changed

I cannot make this stuff up!  Once I renamed my event procedure back to the name you provided it worked great!  Thank you very much.  


Author Closing Comment

ID: 40395783
Kyle's code was perfect.  My language converter played games with me, but once I found the issue the code worked great.  Believe it or not, I have been coding ASP.Net web sites since 1.0 and never found a solution to this common problem as elegant as this one.  Great job, Kyle.
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 40396181
Sorry for not responding earlier.  Glad you were able to work through it.  In the future you should tag VB as can be both VB and C#.  I program mostly in C# these days which is why you got that solution.

A good online converter:

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
User art_snob ( encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA.…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question