简体   繁体   English

为什么Class不断抛出“ MyClass的类型初始值设定项引发异常”。

[英]Why Class keeps throwing 'The type initializer for 'MyClass' threw an exception.'

I copied and pasted the methods out of a class from one project and pasted it all into a class in another project. 我从一个项目的类中复制并粘贴了方法,然后将其全部粘贴到另一个项目的类中。 I have searched around and nothing has come up that pertains exactly or close to why in this case it would be throwing the error. 我四处搜寻,没有发现与这种情况会引发错误的原因完全相同或接近的问题。

I made sure that the namespace matched the project, and it keeps throwing 我确保名称空间与项目匹配,并且不断抛出

{"The type initializer for 'MyClass' threw an exception."} {“'MyClass'的类型初始值设定项引发了异常。”}

So then I created another class and left it empty, and when I created an object of it, the page loaded without a problem. 因此,我创建了另一个类并将其留空,当我创建它的对象时,页面加载没有问题。 As soon as I add.. 一旦我添加..

private static string strCn = ConfigurationManager.ConnectionStrings["DDB"].ConnectionString;
private static SqlConnection cn = new SqlConnection(strCn);

it threw the error, but if I comment that out and just add a public variable and a private one and a method 它抛出了错误,但是如果我将其注释掉,只添加一个公共变量和一个私有变量以及一个方法

public int mynum = 1;
private static int num2 = 2;

it runs fine, but any other time I have used 它运行正常,但是其他任何时间我都用过

private static string strCn = ConfigurationManager.ConnectionStrings["DDB"].ConnectionString;
private static SqlConnection cn = new SqlConnection(strCn);

in any of my classes it runs fine. 在我的任何课程中,它运行良好。 So what would be causing the issue? 那么,是什么原因引起的呢? I even manually entered in the private sqlconnection and strCn it would cause an error. 我什至手动输入了私有sqlconnection和strCn,这会导致错误。 To no avail. 无济于事。

That is because the initialization of the static variables in your class failed. 这是因为类中static变量的初始化失败。

Read here about the problem and solution. 在此处阅读有关问题和解决方案的信息。

In fact the problem is that the order of initialization can't always be determined correctly, which means that it is possible that the SqlConnection cn is initialized first, which will cause a NullReferenceException because the strCn isn't filled in yet. 实际上,问题在于初始化顺序不能总是正确确定,这意味着SqlConnection cn可能首先被初始化,这将导致NullReferenceException因为还没有填写strCn

My guess based on the limited is that the line 我基于有限的猜测是

private static string strCn = ConfigurationManager.ConnectionStrings["DDB"].ConnectionString;

is throwing the exception. 引发异常。

That could happen if there were no connection string named "DDB", so ConnectionStrings["DDB"] returns null, which you then try to dereference with .ConnectionString . 如果没有名为“ DDB”的连接字符串,则可能发生这种情况,因此ConnectionStrings["DDB"]返回null,然后尝试使用.ConnectionString取消引用。

Try moving the initialization of strCn into a static constructor, breaking out the initialization steps, and stepping through in the debugger. 尝试将strCn的初始化移动到静态构造函数中,分步初始化步骤,然后逐步进入调试器。

public static
{ // Set a breakpoint here, and see what value is assigned to cfg.
    var cfg = ConfigurationManager.ConnectionStrings["DDB"];
    strCn = cfg.ConnectionString;
}

If this turns out to be the issue, I suggest you keep the static constructor so that you can verify that the connection string has a correct value and do appropriate error handling if not. 如果这是问题所在,建议您保留静态构造函数,以便可以验证连接字符串是否具有正确的值,如果没有,请进行适当的错误处理。

ConnectionStringSettingsCollection.Item(string) returns null if no connection with the given name is found, so 如果找不到与给定名称的连接,则ConnectionStringSettingsCollection.Item(string)返回null,因此

ConfigurationManager.ConnectionStrings["DDB"].ConnectionString;

throws a NullReferenceException . 抛出NullReferenceException

You need to fix your configuration to ensure the connection string exists. 您需要修复配置以确保连接字符串存在。

暂无
暂无

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

相关问题 “MyClass”的类型初始值设定项引发异常 - The type initializer for 'MyClass' threw an exception System.TypeInitializationException: 'Bid' 的类型初始值设定项引发异常。 - System.TypeInitializationException: 'The type initializer for 'Bid' threw an exception.' 如何调试“WindowsFormsAutoClick.Form1的类型初始化程序”引发异常。“? - How to debug “Type initializer for 'WindowsFormsAutoClick.Form1' threw an exception.”? “Azure.Core.Pipeline.HttpClientTransport”的类型初始化程序引发了异常。 - The type initializer for “Azure.Core.Pipeline.HttpClientTransport” threw an exception.' System.TypeInitializationException:'类型初始值设定项引发了异常。 - System.TypeInitializationException: 'The type initializer threw an exception.' “Microsoft.AspNetCore.Mvc.MvcCoreLoggerExtensions”的类型初始值设定项引发异常。 - The type initializer for 'Microsoft.AspNetCore.Mvc.MvcCoreLoggerExtensions' threw an exception.' ''的类型初始值设定项引发了异常 - The type initializer for '' threw an exception 类型初始值设定项引发了异常 - The type initializer threw an exception 类型初始值设定项引发异常 - Type initializer threw an exception 我遇到了异常:'mws.Options_DB'的类型初始化器引发了异常。 这是什么意思? - I got exception: The type initializer for 'mws.Options_DB' threw an exception. what does it mean?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM