【问题标题】:NullReferenceException thrown after null check?空检查后抛出 NullReferenceException?
【发布时间】:2014-08-16 07:03:39
【问题描述】:

所以我通过 GUID 跟踪不同用户的访问。到目前为止,它一直运行良好,我不明白为什么它会失败。

我正在分配变量,然后检查 null,然后运行代码,然后它会引发 nullreferenceexception 错误。代码如下:

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

在 aooDB_Items.SElectItemID(GUID) 行中抛出 NullReferenceException

更新:我还尝试设置 GUID = "none",然后在运行代码之前检查以确保它不是 "none"。代码每次都会运行,如果我在任何地方放置断点,我可以看到 GUID 有一个 GUID 作为值,这使得这更加混乱。

错误:

    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.

【问题讨论】:

  • 也发布错误信息
  • 也许aooDB_Items 为空?
  • 您可能正在尝试强制转换为 int 空值。检查 SelectItemID 是否返回有效值。
  • if (!String.IsNullOrEmpty(GUID)) 是多余的。
  • 添加了错误并检查 SelectItemID 是否是问题所在。 SelectItemID 在布局完全相同的不同页面上工作。

标签: c# .net nullreferenceexception


【解决方案1】:

InBetween 和 Robby Cornelissen 帮助我解决了这个问题。问题是 SelectItemID 没有发回任何东西。

【讨论】:

    猜你喜欢
    • 2014-06-15
    • 2021-04-11
    • 1970-01-01
    • 2014-11-25
    • 2011-10-27
    • 1970-01-01
    • 2013-05-30
    • 1970-01-01
    • 2020-11-17
    相关资源
    最近更新 更多