An unhandled exception of type 'System.TypeInitializationException' occurred means what exactly?

Ive googled it and all the answers where all over the place, and none of them about the graphicsdevicemanager, which is apparently causing my error. Can someone point me in the right direction? Here is the code thats causing the problem:

SceneManager class where the error occours:

class SceneManager : GameWorld
{
    // Makes this class a singleton class
    public static readonly SceneManager instance = new SceneManager();

    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;

    public enum MyScenes
    {
        Login,
        Register,
        MainMenu,
        Options,
        AboutTheGame,
        Credits,
        Characters,
        Appearance,
        DesignPlaystyle,
        Talents,
        Rankings,
        Playing,
        PauseMenu,
        Closing,
    }
    public MyScenes myScenes;

    private TalentsScene myTalentsScene;
    private DesignPlayStyleScene myDesignPlaystyleScene;
    private CharacterScene myCharacterScene;
    private MenuScene myMenuScene;

    private Player ourPlayer;

    public SceneManager()
    {
        graphics = new GraphicsDeviceManager(this); // <---- Error here

    }

    protected override void Initialize()
    {
        myScenes = MyScenes.DesignPlaystyle;


        myTalentsScene = new TalentsScene(graphics);
        myDesignPlaystyleScene = new DesignPlayStyleScene(graphics);
        myCharacterScene = new CharacterScene(graphics);
        myMenuScene = new MenuScene(graphics);

        base.Initialize();
    }

GameWorld:

public class GameWorld : Game
{
    // Makes this class a singleton class
    public static readonly GameWorld instance = new GameWorld();

    public enum ClassBtnToggled
    {
        None,
        Amazonian,
        Magus,
        ShadowSpawn,
        Blackguard,
        SenjuMonk,
        Deadeye,
        Gladiator,
        Templar,
    }
    public ClassBtnToggled classBtnToggled;

    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;

    private SceneManager mySceneManager;

    public int screenWidth = 1360;
    public int screenHeight = 768;
    public string chosenClass;
    SpriteFont myFont;
    private const float messageDelay = 2;
    private float remainingDelay = messageDelay;
    private string designPlaystyleMessages;


    public GameWorld()
    {
        graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";

        graphics.PreferredBackBufferWidth = screenWidth;
        graphics.PreferredBackBufferHeight = screenHeight;
        graphics.ApplyChanges();
        IsMouseVisible = true;

    }

    protected override void Initialize()
    {
        mySceneManager = SceneManager.instance;

        classBtnToggled = ClassBtnToggled.None;

        base.Initialize();
    }


    protected override void LoadContent()
    {
        spriteBatch = new SpriteBatch(GraphicsDevice);
        mySceneManager.LoadContent();

        //myFont = Content.Load<SpriteFont>("myFont"); /*asdadasd*/

    }


    protected override void UnloadContent()
    {
    }

    protected override void Update(GameTime gameTime)
    {
        mySceneManager.Update(gameTime);

        base.Update(gameTime);
    }

    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.White);
        spriteBatch.Begin();

        mySceneManager.Draw(gameTime);

        spriteBatch.End();
        base.Draw(gameTime);
    }

You’re doing something really weird by having SceneManager inherit from GameWorld and having a reference from GameWorld to a SceneManager. It really doesn’t make sense to have the SceneManager inherit from GameWorld. Rule of thumb is to check if it makes sense to say X is a Y where X is the inheriting class. In this case it doesn’t make sense to say that a SceneManager is a GameWorld.

Anyway, you’re getting an error because GameWorld already made a GraphicsDeviceManager. You can’t make another one. There should be an inner exception that says exactly that. You already have access to the existing GraphicsDeviceManager from GameWorld in SceneManager because it inherits from it (though as I mentioned, that doesn’t make a lot of sense). A better structure would be to have your initial Game class either be a SceneManager or delegate to a SceneManager (since Game is your entry point, and you don’t start out with a GameWorld, but with a SceneManager that can manage the scene with the gameworld) and have GameWorld be part of a Scene that the SceneManager can control or something like that.

1 Like

Yeah im aware this is a very silly solution, which came about after a series of problems. Im gonna go back to my original structure and see if i can make it work with that method you just mentioned. Thanks! ^^

1 Like