简体   繁体   中英

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."}

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. To no avail.

That is because the initialization of the static variables in your class failed.

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.

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 .

Try moving the initialization of strCn into a static constructor, breaking out the initialization steps, and stepping through in the debugger.

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

ConfigurationManager.ConnectionStrings["DDB"].ConnectionString;

throws a NullReferenceException .

You need to fix your configuration to ensure the connection string exists.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM