空检查后的NullReferenceException抛出? [英] NullReferenceException thrown after null check?

查看:188
本文介绍了空检查后的NullReferenceException抛出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我跟踪不同用户通过GUID访问。它曾大到现在为止,我不明白为什么它的失败。

我赋予该变量,然后我检查空,然后运行code,然后把它抛出NullReferenceException异常错误。这里是code:

 如果(String.IsNullOrEmpty(会话[GUID]作为字符串))
    {
        Server.Transfer的(〜/的Index.aspx,真正的);
    }
    其他
    {
        GUID =会话[GUID]的ToString()。
        如果(!String.IsNullOrEmpty(GUID))
        {
            ITEMID =(INT)aooDB_Items.SelectItemID(GUID);
        }
    }
 

该NullReferenceException异常被抛出在aooDB_Items.SElectItemID(GUID)行

更新:我也尝试设置GUID =无,然后检查,以确保它不是无的办code之前。在code运行在每个时间,如果我把一个断点在任何地方我可以看到GUID都有一个GUID为一个值,这使得这更混乱。

错误:

 键入System.NullReferenceException的异常App_Web_3hq5xlxh.dll发生,但在用户code没有处理

    其他信息:未将对象引用设置到对象的实例。
 

解决方案

插图中和罗比Cornelissen帮我解决这个问题。问题是,SelectItemID被发回什么。

So I track different users access through GUID. It has worked great up until now and I dont understand why its failing.

I am assigning the variable, then I check for null, then run the code, and then it throws the nullreferenceexception error. Here is the code:

    if (String.IsNullOrEmpty(Session["GUID"] as string))
    {
        Server.Transfer("~/index.aspx", true);
    }
    else
    {
        GUID = Session["GUID"].ToString();
        if (!String.IsNullOrEmpty(GUID))
        {
            itemID = (int)aooDB_Items.SelectItemID(GUID);
        }
    }

The NullReferenceException is being thrown in the aooDB_Items.SElectItemID(GUID) line

Update: I also tried setting GUID = "none" and then checking to make sure its not "none" before running the code. The code runs each time and if I put a breakpoint in anywhere I can see that GUID has a GUID as a value which makes this more confusing.

Error:

    An exception of type 'System.NullReferenceException' occurred in App_Web_3hq5xlxh.dll but was not handled in user code

    Additional information: Object reference not set to an instance of an object.

解决方案

InBetween and Robby Cornelissen helped me solve this problem. The issue was that SelectItemID was sending back nothing.

这篇关于空检查后的NullReferenceException抛出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆