类型初始值设定项异常 - C#

发布于 2024-07-13 15:39:23 字数 813 浏览 6 评论 0原文

namespace X{  public static class URLs
{
    public static TabController tabIdLookUp = new TabController();
    public static string DASHBOARD_AUDIT_PAGE = tabIdLookUp.GetTabByName("View My Safety", 2).TabID.ToString();
    public static string URL_GENERATE_WITH_MID(String TabName, int PortalId){        {
        return tabIdLookUp.GetTabByName(TabName, PortalId).TabID.ToString();
    }
}}

... 在我的用户控件中,我这样做:

Response.Redirect("/" + X.URLs.URL_GENERATE_WITH_MID("test", 1)); // this causes the error

错误是:“X.URLs”的类型初始值设定项引发异常。 ---> System.NullReferenceException:未将对象引用设置为对象的实例。 at X.URLs..cctor()

无法调试,因为它在我的本地机器上工作,但在服务器上抛出该错误。

有任何想法吗?

PS,问题最终变成了一个微不足道的 NUllReferenceException - GetTabByName() 返回 NULL

namespace X{  public static class URLs
{
    public static TabController tabIdLookUp = new TabController();
    public static string DASHBOARD_AUDIT_PAGE = tabIdLookUp.GetTabByName("View My Safety", 2).TabID.ToString();
    public static string URL_GENERATE_WITH_MID(String TabName, int PortalId){        {
        return tabIdLookUp.GetTabByName(TabName, PortalId).TabID.ToString();
    }
}}

...
in my user control i do this:

Response.Redirect("/" + X.URLs.URL_GENERATE_WITH_MID("test", 1)); // this causes the error

the error is: The type initializer for 'X.URLs' threw an exception. ---> System.NullReferenceException: Object reference not set to an instance of an object. at X.URLs..cctor()

can't debug because it works on my local box, but throws that error on the server.

any ideas?

P.S. the problem ended up being a trivial NUllReferenceException - GetTabByName() was returing NULL

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜还是长夜 2024-07-20 15:39:23

与其让“DASHBOARD AUDIT PAGE”的初始化程序直接引用 tabIdLookUp,为什么不在静态构造函数中初始化这两个变量并查看是否可以修复错误?

namespace X{  public static class URLs
{
    public static TabController tabIdLookUp;
    public static string DASHBOARD_AUDIT_PAGE;
    public static string URL_GENERATE_WITH_MID(String TabName, int PortalId){        {
        return tabIdLookUp.GetTabByName(TabName, PortalId).TabID.ToString();
    }

    static URLs() {
        tabIdLookUp = new TabController();
        DASHBOARD_AUDIT_PAGE = tabIdLookUp.GetTabByName("View My Safety", 2).TabID.ToString();
    }
}}

您可能遇到的另一个问题是,如果 GetTabByName 返回 NULL 引用,则您无法防止这种情况,而只是引用 .TabID 属性。 您应该确保在引用该属性之前获得有效的引用。

Rather than having your initializer for "DASHBOARD AUDIT PAGE" refer to tabIdLookUp directly, why not instead initialize both of those variables in a static constructor and see if that fixes the error?

namespace X{  public static class URLs
{
    public static TabController tabIdLookUp;
    public static string DASHBOARD_AUDIT_PAGE;
    public static string URL_GENERATE_WITH_MID(String TabName, int PortalId){        {
        return tabIdLookUp.GetTabByName(TabName, PortalId).TabID.ToString();
    }

    static URLs() {
        tabIdLookUp = new TabController();
        DASHBOARD_AUDIT_PAGE = tabIdLookUp.GetTabByName("View My Safety", 2).TabID.ToString();
    }
}}

Another problem you could be having is if GetTabByName is returning a NULL reference, you're not protecting against that and just referencing the .TabID property. You should probably ensure that you're getting back a valid reference before referring to the property.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文