Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"TypeInitializationException was unhandled"

Error Log:

System.TypeInitializationException was unhandled
  Message="The type initializer for 'MaxDavidMP4.Program' threw an exception."
  Source="MaxDavidMP4"
  TypeName="MaxDavidMP4.Program"
  StackTrace:
       at MaxDavidMP4.Program.Main()
       at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: System.IO.FileNotFoundException
       Message="Could not load file or assembly 'Microsoft.Xna.Framework, Version=3.1.0.0, Culture=neutral, PublicKeyToken=6d5c3888ef60e27d' or one of its dependencies. The system cannot find the file specified."
       Source="MaxDavidMP4"
       FileName="Microsoft.Xna.Framework, Version=3.1.0.0, Culture=neutral, PublicKeyToken=6d5c3888ef60e27d"
       FusionLog="=== Pre-bind state information ===\r\nLOG: User = Max-PC\\Max\r\nLOG: DisplayName = Microsoft.Xna.Framework, Version=3.1.0.0, Culture=neutral, PublicKeyToken=6d5c3888ef60e27d\n (Fully-specified)\r\nLOG: Appbase = file:///C:/Users/Max/Desktop/maximas save/School/University/CSS 450/MaxDavidMP4/MaxDavidMP4/bin/Debug/\r\nLOG: Initial PrivatePath = NULL\r\nCalling assembly : UWBGL_XNA_Lib10, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null.\r\n===\r\nLOG: This bind starts in default load context.\r\nLOG: No application configuration file found.\r\nLOG: Using machine configuration file from C:\\Windows\\Microsoft.NET\\Framework64\\v2.0.50727\\config\\machine.config.\r\nLOG: Post-policy reference: Microsoft.Xna.Framework, Version=3.1.0.0, Culture=neutral, PublicKeyToken=6d5c3888ef60e27d\r\nLOG: The same bind was seen before, and was failed with hr = 0x80070002.\r\n"
       StackTrace:
            at MaxDavidMP4.Model..ctor()
            at MaxDavidMP4.Program..cctor() in C:\Users\Max\Desktop\maximas save\School\University\CSS 450\MaxDavidMP4\MaxDavidMP4\Program.cs:line 14
       InnerException: 

Program.cs code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace MaxDavidMP4
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        /// 
        static Model model = new Model();

        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }

        public static Model getModel()
        {
            return model;
        }
    }
}

It seems the issue is with Microsoft.Xna.Framework 3.1.0 inclusion, but I'm sure the path is correct, and all my other projects work fine with this setup. This is in VS2008, C#, btw. I remember last time I had this issue, I had to set one of the top central dropdown lists in VS2008 to 'x86' and that fixed it, but now it makes no difference.

like image 245
CodedMonkey Avatar asked Nov 05 '10 22:11

CodedMonkey


2 Answers

It is likely you are having the same problem you had last time. I can't think of another reason you would be getting that exception. It's likely you haven't implemented the fix correctly.

Note that it is the inner exception that matters here. The outer exception would go away if your created your Model in the main body of the program rather than during static initialization.

So your exception is basically:

Could not load assembly 'Microsoft.Xna.Framework'. The system cannot find the file specified.

Shawn Hargreaves writes that the exception really means:

Could not load 32 bit assembly 'Microsoft.Xna.Framework' into a 64 bit process. Your game project is set to 'Any CPU' platform, when it should specify 'x86'.

The fix is:

In your Visual Studio toolbar, there should be a combo box saying 'Any CPU'.

If you are using C# Express, this toolbar entry may be grayed out. To enable it, go to 'Tools / Options', check 'Show all settings', select the 'Projects and Solutions' tab, and check the 'Show advanced build configurations' box.

Pull down the 'Any CPU' toolbar combo, and choose 'Configuration Manager'. Open the 'Active solution platform' combo, choose '<New...>', and create an 'x86' configuration.

like image 150
Empyrean Avatar answered Nov 15 '22 15:11

Empyrean


I had the same error appear right on the first line of my program Main(), which had been working perfectly for days. It had nothing to do with loading assemblies. The problem was I had accidentally pasted in a call to a static utility class' methods in the declaration section of that same utility class. Example:

public class Utility
{
    public static int i = Utility.SomeStaticMethod();

    public static int SomeStaticMethod()
    {
        return 1;
    }
}

I don't totally understand the inner guts of what happens to the above, but I know it was the problem because as soon as I moved that call into an Init() method, the TypeInitializationException disappeared. My guess is while the above is legal from a compiler POV, at runtime it encountered usage of a method that was not yet defined. Since this is used as a totally static class, the class is not "initialized" within the same scope as everything else but somehow initialized behind the scenes where normal exception flow isn't available.

Thoughts?

like image 26
John M. Black Avatar answered Nov 15 '22 17:11

John M. Black