32
votes

My Windows Forms application was working earlier, however suddenly it stopped working. I am getting following exception:

enter image description here

With exception details as follows:

System.TypeInitializationException was unhandled
Message: An unhandled exception of type 'System.TypeInitializationException' occurred in mscorlib.dll
Additional information: The type initializer for 'NotificationTester.Program' threw an exception.

When I click OK, the VS windows then shows following:

enter image description here

The solution was working fine earlier. I don't get whats going wrong.

13
for TypeInitializationException you need to check the InnerException property for more detailsSriram Sakthivel
Well I know I should look for InerExceotion whenever exception occurs by clicking on Exception Details link on the Exception pop up dialog box as shown here. Point is my code does not enter Program.Main() So I am not able to debug my code. I hit F5 and get this exception. And unfortunately this exception dialog does not have View Details link. So I think down vote on this question will be removed.Mahesha999
Ohh its static Program() constructor as well recognized by @Marc Gravell below, wish the exception description could have told me that.Mahesha999

13 Answers

45
votes

So: either one of the field-initializers, or the static constructor, for Program - is failing. Find out why. Note: the InnerException has the actual exception that was raised, but basicaly: just debug the field initializers and static constructor. So look inside the Program class for either:

static SomeType someField = /* some non-trivial expression or method call */ 

or:

static Program() {
    // stuff
}
14
votes

Another possible reason: the app.config has duplicate sections.

9
votes

A possible reason: init a static dictionary with duplicated keys.

7
votes

I got the same error message and for my case the reason is my main program is set to build as 32 bit console app and I added a private variable logger which access a 64bit dll.

public class Program
{
    public static readonly Logger logger = new Logger(typeof(Program));

After I changed the main program to be build as 64bit, the issue is fixed.

3
votes

In my case the reason was, that <configSections> wasn't first one in config file.

Only one <configSections> element allowed per config file and if present must be the first child of the root <configuration> element.

Move the configSections element to the top in your config file.

Hope it helps to someone.

1
votes

I've got the same error with platform target set as any CPU and prefer 32-bit checked, unchecking that last one solved my problem.

1
votes

This is one weird issue I had to deal with for the last 2 hours. I solved it by removing the static from the lists I created.

private static readonly List<Person> someList = GlobalConfiguration.Connection.PopulateList();

with this one:

private readonly List<Person> someList = GlobalConfiguration.Connection.PopulateList();

Hope it helps and you don't have to spend two hours to find out the bug...

1
votes

Make sure you are not missing any dependency DLLs. In my case, a DLL I was referencing had a dependency on another DLL.

If this is what happened, look at the "Inner Exception" property and then you will see a better error message. In my case, it said "Cannot find xxx.dll"

1
votes

So, If you didn't get an innerException message like me, you can set a breakpoint on some of the static variables like strings that you may have declared. Then you can debug forwards from there with F10.

1
votes

In my case it was a very silly thing. I had accidentally typed something like this

<configuration>
    <appSettings>
         <add key="someKey" value="SomeValue" />sss
         <add key="someKey1" value="SomeValue1" />
    </appSettings>
</configuration> 

sss - Caused the error for me. Any random character entry.

0
votes

This issue for me was caused by a rogue user.config file that was created in the AppData\Local[Manufacturer][Product Name] directory. I'm not sure how it got there, but seems to be created every now and then.

0
votes

After trying all of the answers listed here, I have a new one:

The type initializer for 'MyLibrary' threw an exception.

If you see something like the below in the InnerException(s)..........

"Could not load file or assembly 'log4net, Version=1.2.13.0, Culture=neutral, PublicKeyToken=669e0ddf0bb1aa2a' or one of its dependencies. The system cannot find the file specified.":"log4net, Version=1.2.13.0, Culture=neutral, PublicKeyToken=669e0ddf0bb1aa2a"

In my case, my directory had the correct version dll. (log4net.dll in this case).

And then ... the issue was found. Assembly redirects in the app.config.

:(

Since I had the correct version, I removed all my redirects. Your situation may be different.

  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="log4net" publicKeyToken="b32731d11ce58905" />
        <codeBase version="1.2.9.0" href="log4netv1.2.9.0\log4net.dll" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="log4net" publicKeyToken="1b44e1d426115821" />
        <codeBase version="1.2.10.0" href="log4netv1.2.10.0\log4net.dll" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="log4net" publicKeyToken="669e0ddf0bb1aa2a" />
        <codeBase version="1.2.13.0" href="log4netv1.2.13.0\log4net.dll" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
0
votes

I've the same problem: runs program in MS VS 2015 and triggers an exception message:

System.TypeInitializationException was unhandled
Message: An unhandled exception of type 'System.TypeInitializationException' occurred in mscorlib.dll
Additional information: Der Typeninitialisierer für "<Module>" hat eine Ausnahme verursacht.

The question where source this exception? So, I started the EXE file and displayed the exception message dialog. Answer on "debug mode" with MS VS so become more information displayed about exception.

System.IO.FileNotFoundException was unhandled
    Message: An unhandled exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.dll
    Additional information: Die Datei oder Assembly "SIC, Version=19.2.6.3, Culture=neutral, PublicKeyToken=**************" oder eine Abhängigkeit davon wurde nicht gefunden. Das System kann die angegebene Datei nicht finden.

I go to this reference and analyse properties Property: "Copy Local" is on false - because before was a installed into GAC Solution: property "Copy Local" set on true and builds new assembly and works :-)

Maybe this information helpful! So have a good day.