Niar
Niar

Reputation: 540

Getting error "Object reference not set to an instance of an object"

In the below code i got an error "Object reference not set to an instance of an object" in the line of the 'if' condition. Can any one help me with what is wrong with my code.

public string MemberLogOut()
    {
        string ret = string.Empty;
        try
        {
            if (HttpContext.Current.Session.Count > 0)
            HttpContext.Current.Session.Clear();
           ret="1";
        }
        catch (SqlException ex)
        {
            throw new Exception(ex.Message);
            ret="2";
        }
        return ret;
    }

Upvotes: 1

Views: 25739

Answers (3)

Dperish
Dperish

Reputation: 36

As long as you have System.Web referenced in your using statements, then you should be able to use this:

if (Session != null) {Session.Clear();}

Or

if (Session != null) {Session.Abandon();}

Im not sure why you would you return a string which holds an integer though. A boolean value would make more sense, but you really shouldn't need anything in this context.

Also, your exception handler is attempting to catch a sqlexception, which could also be a source of an object reference error, as you don't appear to have any SQL objects in this function.

I'd probably do this following:

protected bool MemberLogOut()
{
    try {
        if (Session != null) {Session.Abandon();}
        //do any logging and additional cleanup here
        return true;
    } catch {
        return false;
    }
}

Edit: if you are in fact calling from outside of your web project, you can just pass the current httpcontext to the following method:

protected bool MemberLogOut(HttpContext context)
{
    try {
        if (context != null && context.Session != null) {
            context.Session.Abandon();
        }
        //do any logging and additional cleanup here
        return true;
    } catch (Exception ex) {
        //log here if necessary
        return false;
    }
}     

Upvotes: 2

Frank59
Frank59

Reputation: 3261

try that code

public string MemberLogOut()
{
    string ret = string.Empty;
    try
    {
        if (HttpContext.Current.Session!=null)
        {HttpContext.Current.Session.Clear();}

    }
    catch (SqlException ex)
    {
        throw new Exception(ex.Message);
    }
    return "1";
}

Upvotes: -1

Darin Dimitrov
Darin Dimitrov

Reputation: 1038730

Can any one help me with what is wrong with my code

I guess that you are running this code outside an ASP.NET application. HttpContext.Current exists only inside the context of a web application. If you ever attempt to run this code outside (for example in a console, desktop, unit test, ...) it's never gonna work.

So if this is some sort of code that is in a class library intended to be reused across different applications you will have to remove the dependency on the HttpContext from it.

Side remark: your if condition seems kinda useless as you are doing exactly the same thing in the else as well as in the if -> clearing the session.

Upvotes: 1

Related Questions