MonoGame/MonoGame · error · ArgumentNullException

Game cannot be null.

Error message

Game cannot be null.

What it means

Thrown by the GraphicsDeviceManager constructor when the supplied Game instance is null. MonoGame requires a live Game to attach the manager to (it immediately reads _game.Window.ClientBounds, registers services, etc.), so a null game is an invalid state, not a recoverable one.

Source

Thrown at MonoGame.Framework/GraphicsDeviceManager.cs:58

        /// <summary>
        /// The default back buffer height.
        /// </summary>
        public static readonly int DefaultBackBufferHeight = 480;

        /// <summary>
        /// Optional override for platform specific defaults.
        /// </summary>
        partial void PlatformConstruct();

        /// <summary>
        /// Associates this graphics device manager to a game instances.
        /// </summary>
        /// <param name="game">The game instance to attach.</param>
        public GraphicsDeviceManager(Game game)
        {
            if (game == null)
                throw new ArgumentNullException("game", "Game cannot be null.");

            _game = game;

            _supportedOrientations = DisplayOrientation.Default;
            _preferredBackBufferFormat = SurfaceFormat.Color;
            _preferredDepthStencilFormat = DepthFormat.Depth24;
            _synchronizedWithVerticalRetrace = true;

            // Assume the window client size as the default back
            // buffer resolution in the landscape orientation.
            var clientBounds = _game.Window.ClientBounds;
            if (clientBounds.Width >= clientBounds.Height)
            {
                _preferredBackBufferWidth = clientBounds.Width;
                _preferredBackBufferHeight = clientBounds.Height;
            }
            else
            {

View on GitHub (pinned to 1d71bbd0ff)

Solutions

  1. Ensure the Game instance is non-null before constructing the manager; in the Game constructor pass `this`.
  2. If the manager is built outside the Game, thread the Game instance through explicitly rather than relying on a nullable field.
  3. Add a null check at the call site so the failure points at the real source instead of deep inside MonoGame.

Example fix

// before
_graphics = new GraphicsDeviceManager(_game); // _game is null here

// after
if (_game == null) throw new InvalidOperationException("Game must be created first.");
_graphics = new GraphicsDeviceManager(_game);
Defensive patterns

Strategy: validation

Validate before calling

if (game == null) throw new InvalidOperationException("Game instance is required before creating GraphicsDeviceManager.");
var gdm = new GraphicsDeviceManager(game);

Type guard

static bool HasValidGame(Game game) => game != null && game.Window != null;

Prevention

When it happens

Trigger: Constructing `new GraphicsDeviceManager(null)` directly, or passing a field/property that has not yet been assigned a Game instance (e.g. calling it from a static context or before the Game field is initialized).

Common situations: Calling the graphics device manager setup from a helper/static method that lost the Game reference; DI/migration where the Game instance is injected but still null during early startup; refactors that moved the `new GraphicsDeviceManager(this)` call out of the Game constructor.

Related errors


AI-assisted analysis of MonoGame/MonoGame@1d71bbd0ff (2026-08-13). Data as JSON: /api/errors/414c15d084df0298. Report an issue: GitHub.