简体   繁体   English

类型初始化器异常-C#

[英]Type initializer exception - C#

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. 错误是:'X.URLs'的类型初始值设定项引发了异常。 ---> System.NullReferenceException: Object reference not set to an instance of an object. ---> System.NullReferenceException:对象引用未设置为对象的实例。 at X.URLs..cctor() 在X.URLs..cctor()

can't debug because it works on my local box, but throws that error on the server. 无法调试,因为它可以在我的本地机器上运行,但会在服务器上抛出该错误。

any ideas? 有任何想法吗?

PS the problem ended up being a trivial NUllReferenceException - GetTabByName() was returing NULL PS问题最终成为一个琐碎的NUllReferenceException-GetTabByName()重现NULL

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? 与其直接使用“ 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();
    }
}}

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. 您可能遇到的另一个问题是,如果GetTabByName返回的是NULL引用,那么您就不能对此加以保护,而只是引用.TabID属性。 You should probably ensure that you're getting back a valid reference before referring to the property. 您可能应该确保在引用该属性之前,先获取有效的引用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在c#中输入初始化程序异常 - Type initializer exception in c# C#错误“…的类型初始值设定项引发了异常 - C# Error "The type initializer for … threw an exception TFS C#-加载workItemStore返回null或键入初始化器异常 - TFS C# - Loading workItemStore returns null or type initializer exception 错误:Daemon.Global的类型初始化器在c#中引发异常 - Error : The Type Initializer of Daemon.Global threw an exception in c# C#动态类型初始化程序 - C# Dynamic Type Initializer 使用c#asp.net连接到Firebird数据库会引发类型初始化器异常 - Connect to Firebird database with c# asp.net throws type initializer exception 从C#错误调用Matlab函数错误:“ tes.tambah”的类型初始值设定项引发了异常 - Call Matlab Function from C# error : The type initializer for 'tes.tambah' threw an exception ClosedXML 读取 excel 文件错误:“MS.Utility.EventTrace”的类型初始化程序引发异常。 (C#) - ClosedXML Read excel file Error: The type initializer for 'MS.Utility.EventTrace' threw an exception. (C#) C#“ Google.Apis.Json.NewtonsoftJsonSerializer”的类型初始值设定项引发了异常 - C# The type initializer for 'Google.Apis.Json.NewtonsoftJsonSerializer' threw an exception “ crystaldecisions.shared.sharedutils”的类型初始值设定项引发了异常。 在C#中 - The type initializer for 'crystaldecisions.shared.sharedutils' threw an exception. in c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM