NullReferenceException thrown after null check?

爷,独闯天下 提交于 2019-12-11 08:51:31

问题


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.

回答1:


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



来源:https://stackoverflow.com/questions/25337791/nullreferenceexception-thrown-after-null-check

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!