36

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.

3
  • 1
    for TypeInitializationException you need to check the InnerException property for more details Commented Jul 25, 2014 at 11:47
  • 3
    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. Commented Jul 25, 2014 at 12:29
  • Ohh its static Program() constructor as well recognized by @Marc Gravell below, wish the exception description could have told me that. Commented Jul 25, 2014 at 12:34

16 Answers 16

49

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
}
Sign up to request clarification or add additional context in comments.

Comments

14

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

3 Comments

Yip, had the same issue. Was duplication in app.config (bad copy paste). Had applicationSettings tag duplicated. <applicationSettings> <applicationSettings> <MyProject.Properties.Settings> <setting name="xxxx" serializeAs="String"> <value>....</value> </setting> </MyProject.Properties.Settings> </applicationSettings> ..... </applicationSettings>
That was it for me too! I checked my source control history and saw a change. I removed the change and all is well now. We'll need to throw @Stagg a party.
I had this problem when I added an <appSettings> element to a config that was already using <applicationSettings>
9

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

Comments

7

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.

Comments

3

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.

Comments

2

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.

Comments

1

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.

Comments

1

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

Comments

1

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"

Comments

1

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 Comment

This was it. I usually set all my globals on top, and always initialize them in the constructor. Lately I've been changing it up, and forgot to check there first.
1

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 "discovered". Gaaaaa!! Assembly redirects in the app.config.

:(

Since I had the correct version of the dll file, 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>

Comments

0

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.

Comments

0

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.

Comments

0

The reason is the app.config/Web.config has been changed in some sections.

Comments

0

Another possible reason:
Let's say you have such a property:

static Dictionary<string, ComplexObject> Objects = new Dictionary<string, ComplexObject>()
{
  {"key1", new ... } // again complex initializer object
}

In case you use the Objects property inside the initializer object, such an exception will raise.
It makes sense: There is no way to initialize a property using its own value which is not yet been initialized!

Comments

0

My case is I added comment in config file <!---->

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.