{"record":{"id":"414c15d084df0298","repo":"MonoGame/MonoGame","slug":"game-cannot-be-null","errorCode":null,"errorMessage":"Game cannot be null.","messagePattern":"Game cannot be null\\.","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"MonoGame.Framework/GraphicsDeviceManager.cs","lineNumber":58,"sourceCode":"\n        /// <summary>\n        /// The default back buffer height.\n        /// </summary>\n        public static readonly int DefaultBackBufferHeight = 480;\n\n        /// <summary>\n        /// Optional override for platform specific defaults.\n        /// </summary>\n        partial void PlatformConstruct();\n\n        /// <summary>\n        /// Associates this graphics device manager to a game instances.\n        /// </summary>\n        /// <param name=\"game\">The game instance to attach.</param>\n        public GraphicsDeviceManager(Game game)\n        {\n            if (game == null)\n                throw new ArgumentNullException(\"game\", \"Game cannot be null.\");\n\n            _game = game;\n\n            _supportedOrientations = DisplayOrientation.Default;\n            _preferredBackBufferFormat = SurfaceFormat.Color;\n            _preferredDepthStencilFormat = DepthFormat.Depth24;\n            _synchronizedWithVerticalRetrace = true;\n\n            // Assume the window client size as the default back\n            // buffer resolution in the landscape orientation.\n            var clientBounds = _game.Window.ClientBounds;\n            if (clientBounds.Width >= clientBounds.Height)\n            {\n                _preferredBackBufferWidth = clientBounds.Width;\n                _preferredBackBufferHeight = clientBounds.Height;\n            }\n            else\n            {","sourceCodeStart":40,"sourceCodeEnd":76,"githubUrl":"https://github.com/MonoGame/MonoGame/blob/1d71bbd0ff4071a03caa583aa3bc56b85924a819/MonoGame.Framework/GraphicsDeviceManager.cs#L40-L76","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","solutions":["Ensure the Game instance is non-null before constructing the manager; in the Game constructor pass `this`.","If the manager is built outside the Game, thread the Game instance through explicitly rather than relying on a nullable field.","Add a null check at the call site so the failure points at the real source instead of deep inside MonoGame."],"exampleFix":"// before\n_graphics = new GraphicsDeviceManager(_game); // _game is null here\n\n// after\nif (_game == null) throw new InvalidOperationException(\"Game must be created first.\");\n_graphics = new GraphicsDeviceManager(_game);","handlingStrategy":"validation","validationCode":"if (game == null) throw new InvalidOperationException(\"Game instance is required before creating GraphicsDeviceManager.\");\nvar gdm = new GraphicsDeviceManager(game);","typeGuard":"static bool HasValidGame(Game game) => game != null && game.Window != null;","tryCatchPattern":null,"preventionTips":["Always create the GraphicsDeviceManager inside the Game constructor using `this`.","Never pass a possibly-null Game field to the constructor.","Static helpers that build the manager must take Game as a required parameter."],"tags":["monogame","graphics","argument-null","startup"],"backgroundTag":null,"analyzedSha":"1d71bbd0ff4071a03caa583aa3bc56b85924a819","analyzedAt":"2026-08-13T17:10:33.625Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}