[英]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.