【问题标题】:ViewState is NULL in Page_Load but not postbackPage_Load 中的 ViewState 为 NULL 但不是回发
【发布时间】:2011-02-27 20:00:47
【问题描述】:

我在公司托管的高流量网站上遇到了一个奇怪的 NullReferenceException 问题。异常记录了完整的堆栈跟踪,但我无法重现它。

该异常每天发生几次,针对不同的用户,它是在下面的代码块中抛出的 NullReferenceException:

protected void Page_Load(object sender, EventArgs e)
{
    ...

    if (!Page.IsPostBack)
    {
        ...
        this.ViewState[StaticClass.StaticStringProperty] = StaticClass.StaticIntProperty; // this is the line the exception occurs on
        ...
    }
}

我唯一能想到会抛出 NullReferenceException 的地方是 ViewState 是否为 NULL,但我从来不知道,也找不到任何原因导致 Page_Load 中的情况不是这样回发。

StaticStringPropertyStaticIntProperty 都是 StaticClass 的已初始化静态属性。

StaticStringProperty 定义为:

public const string StaticStringProperty = "IdFromClient";

有谁知道这是怎么发生的,或者有任何其他原因会在上面的行中抛出 NullReferenceException 吗?

编辑

根据要求,错误的完整堆栈跟踪如下。第 54 行是我在上面突出显示的行。

at MyCompany.MyApplication.Appliance.Page_Load(Object sender, EventArgs e) in C:\Documents and  Settings\shellama\My Documents\MyApplication\Appliance.aspx.cs:line 54
   at System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e)
   at System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e)
   at System.Web.UI.Control.OnLoad(EventArgs e)
   at MyCompany.MyApplication.PageBase.OnLoad(EventArgs e) in C:\Documents and Settings\shellama\My Documents\MyApplication\App_Code\PageBase.cs:line 58
   at System.Web.UI.Control.LoadRecursive()
   at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

【问题讨论】:

  • 你是什么意思为什么你说这两个属性都被初始化了?它们的值是从应用程序中的另一个页面/类设置的吗?即使您认为它无关紧要,您仍然应该提供相关属性。
  • 我已经编辑了我的问题以提供StaticStringProperty 的声明——它是一个永远不会改变的硬编码字符串。 StaticIntProperty 是一个 int 所以永远不会为空。
  • @Andy - 我现在明白你的意思了。我和你一样迷茫!
  • 嗨,安迪,很多时候我都知道在哪一行抛出了空指针异常,结果发现我错了。因此,您可能希望发布整个代码块以查找它可能发生在哪一行。
  • @chiccodoro,感谢您的建议。我们将 PDB 留在生产环境中,它会在堆栈跟踪中准确指出这一行。

标签: c# asp.net viewstate nullreferenceexception


【解决方案1】:

我唯一能说的(我最近也遇到过类似的情况)是StaticClass.StaticStringProperty 是NULL。但是话又说回来,您必须注意这一点。我想不出别的了。

【讨论】:

  • 是的,这是我能想到的唯一其他事情,但它是一个硬编码字符串。我编辑了我的问题以提供StaticStringProperty 的声明。 StaticIntProperty 是一个 int 所以永远不会为空。
  • 也许你可以给堆栈跟踪看看。
  • 我已经发布了堆栈跟踪。我在那里看不到任何特别有用的东西,但请随意看看。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-25
  • 1970-01-01
相关资源
最近更新 更多