?
Solved

Session.Remove giving me the error Object reference not set to an instance of an object.

Posted on 2012-09-11
8
Medium Priority
?
1,374 Views
Last Modified: 2012-09-22
I have an application on a shared public server this problem does not seeem to happen on my local development serer.

The problem I’m having is when I’m checking if a Session Exists

I have this code
 
    public void RemoveCompetitionSessions()
    {
        if (HttpContext.Current.Session["dtblCompetitionInformation"] != null)
            HttpContext.Current.Session.Remove("dtblCompetitionInformation");
        if (HttpContext.Current.Session["dtblCompetitionsWithCompetitionID"] != null)
            HttpContext.Current.Session.Remove("dtblCompetitionsWithCompetitionID");
        if (HttpContext.Current.Session["dtblCompetitionInformation"] != null)
            HttpContext.Current.Session.Remove("dtblCompetitionInformation");
        if (HttpContext.Current.Session["dtblCompetitions"] != null)
            HttpContext.Current.Session.Remove("dtblCompetitions");
    }

Open in new window


As you can see i check to see if the Session exists before trying to remove the session

Some of the Sessions I’m trying to remove are not being used by any of the Methods in the scheduled task.

this void call above occurs on a scheduled task  at 12pm everyday and because this is an application under development there is no traffic to start all the session that I’m trying to remove, only a couple of the sessions are being used by the scheduled task.

I get the error Object reference not set to an instance of an object. But the stack trace does not give me any other details such as which line the error occurred on. Just the path of the methods used which lead to the RemoveCompetitionSessions methods being called.

Stack Trace
at CompetitionActions.RemoveCompetitionSessions() at CompetitionActions.DrawCompetition(SqlConnection conQTXT, SqlTransaction stranAddEntries, DataTable dtblCompetitionInformation, DataTable dtblCompanyInformation, Int32 intCompetitionID, Object objIncentiveCompetitionID, Int32 intCompetitionLiveID, Object objIncentiveCompetitionLiveID, Boolean booForceDraw) at CompetitionActions.DrawOneOffCompetitions(Int32 intCompetitionID, Boolean IsFinished, Boolean IsForcedOrAutomated, SqlConnection conQTXT, SqlTransaction stranAddEntries) at QTXT.ApplicationCode.MaintenanceTasks.AlertsAndCompletitions()

Does anybody know why i might be getting an Object reference not set to an instance of an object error even after i have checked to see it the object is null?
0
Comment
Question by:johngrant_ks
8 Comments
 
LVL 7

Expert Comment

by:gopaltayde
ID: 38389914
Not the exact solution you are looking for but you may try using Session.Clear();
0
 
LVL 9

Expert Comment

by:sarabhai
ID: 38390023
If !( HttpContext.Current.Session["dtblCompetitionInformation"] Is Not Nothing)
    HttpContext.Current.Session.Remove("dtblCompetitionInformation");
0
 
LVL 8

Expert Comment

by:vinhnl
ID: 38391859
You sould check if this key exists in session:

if (Session.ContainsKey("foo")) Session.Remove("foo");

Another choice:

if(Session["foo"] != null) Session["foo"] = null;
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 18

Expert Comment

by:Rajar Ahmed
ID: 38392177
You Can check with them , whether they have done something like removing session from httpmodules which can also result on the above error .
<httpModules>
         <remove name="Session" />
</httpModules>

Open in new window

http://www.codeproject.com/Tips/264083/Performance-Tuning-though-Pipeline-Optimization-in
0
 
LVL 20

Expert Comment

by:informaniac
ID: 38392364
I do not feel the error is from any of these lines.
public void RemoveCompetitionSessions()
    {
        if (HttpContext.Current.Session["dtblCompetitionInformation"] != null)
            HttpContext.Current.Session.Remove("dtblCompetitionInformation");
        if (HttpContext.Current.Session["dtblCompetitionsWithCompetitionID"] != null)
            HttpContext.Current.Session.Remove("dtblCompetitionsWithCompetitionID");
        if (HttpContext.Current.Session["dtblCompetitionInformation"] != null)
            HttpContext.Current.Session.Remove("dtblCompetitionInformation");
        if (HttpContext.Current.Session["dtblCompetitions"] != null)
            HttpContext.Current.Session.Remove("dtblCompetitions");
    }

Open in new window


Have you written anything else inside this method?
0
 

Author Comment

by:johngrant_ks
ID: 38393220
Thanks all for taking time to answer

Informaniac, no there is no more code in the method.
meeran03's   do you mean ask the people i have the server with? if they have code that you have posted see below, because according to the article you point to (Awesome article!) they referrer to that code as being in my web application.

 <httpModules>
         <remove name="Session" />
</httpModules>

Open in new window


vinhnl I think if all else fails i will use the session = null solution, also I did find info on the ContainsKey aspect of sessions in the dictionaries but could not implement it in my asp.net web application, not sure but is this for windows apps?

sarabhai I think you may have the language wrong, I believe you are using VB in your example where I’m using c# :)

gopaltayde hi I might have to use this option, I’m not using to many sessions in my application so I may be able to do a blanket clear of the sessions

Hopefully you can answer the questions i have posed
Regards
John
0
 

Accepted Solution

by:
johngrant_ks earned 0 total points
ID: 38407931
Hi All

Ok have worked out that when i was i sending myself the error in an email it was stripping out the line numbers of the stacktrace where the error was occuring, because i was encoding the string before sending it not sure why thought it would remove the line number, but that another question for another time.

So i was not able to track back to the line where error occured, this once i set up the ablitiy to run a test and step through the code i found the culprit line number  and i turns out that i had to test for HttpContext.Current.Session != null before removing the named session e.g.

if (HttpContext.Current.Session != null)
    if (HttpContext.Current.Session.Remove("Name"))

Regards
John
0
 

Author Closing Comment

by:johngrant_ks
ID: 38424408
The other solutions did not answer the question and i believe that if any one else has this problem then the best solution will be the solution i finally came up with as not to get them chasing down the wrong path.
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses
Course of the Month16 days, 22 hours left to enroll

862 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