简体   繁体   English

获取错误“对象引用未设置为对象的实例”

[英]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. 在下面的代码中,我在“if”条件的行中收到错误“对象引用未设置为对象的实例”。 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;
    }

As long as you have System.Web referenced in your using statements, then you should be able to use this: 只要您在using语句中引用了System.Web,那么您应该能够使用:

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. 此外,您的异常处理程序正在尝试捕获sqlexception,它也可能是对象引用错误的源,因为您在此函数中似乎没有任何SQL对象。

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: 编辑:如果您实际上是从Web项目外部调用,则可以将当前的httpcontext传递给以下方法:

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;
    }
}     

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. 我猜您在ASP.NET应用程序之外运行此代码。 HttpContext.Current exists only inside the context of a web application. HttpContext.Current仅存在于Web应用程序的上下文中。 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. 因此,如果这是某种类库中的某种代码,旨在跨不同的应用程序重用,则必须从中删除对HttpContext的依赖。

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. 旁注:你的if条件似乎有点无用,因为你在else和if - >清除会话时做的完全相同。

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";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM