Search code examples
c#visual-studio-2012xnaopentkmonogame

Type initializer exception OpenTK and monogame


I just installed MonoGame and OpenTK 1.0 and in Visual Studio 2012 I made a new project of type 'Windows OpenGL Game`...but when I run the project I get the following error:

The type initializer for 'OpenTK.Graphics.GraphicsMode' threw an exception.

The error is right on this line (the consturctor, on the base())

public Game1()
    : base() 
{
    graphics = new GraphicsDeviceManager(this);
    Content.RootDirectory = "Content";
}

My laptop has intel oboard graphics 3000 and I think it supports OpenGL...thats the only thing I can would point to. Any ideas?


Solution

  • We can look at the source code and find where the exception went unhandled. The "type initializer" basically means the static constructor:

    static GraphicsMode()
    {
        lock (SyncRoot)
        {
            implementation = Platform.Factory.Default.CreateGraphicsMode();
        }
    }
    

    Unfortunately digging through CreateGraphicsMode doesn't reveal any single obvious source for an exception.

    What you should do now is try to get a stack-trace for that exception, and find out where it originates from within CreateGraphicsMode. The debugger should give you this information when the exception goes unhandled.


    With a small amount of digging, without seeing a stack trace (so I'm pretty much guessing), I came across this potential culprit:

    throw new GraphicsModeException(
        "No GraphicsMode available. This should never happen, please report a bug at http://www.opentk.com");
    

    Which, of course, is extremely unhelpful. Although, based on its location, it would seem to indicate that it cannot find a suitable graphics mode.

    At this stage, I think it would be best to build MonoGame and OpenTK from source so that you can use the debugger to see exactly what they're doing.