Null reference exception trying to call new RenderTarget2D

I am trying to resuscitate an old game project which is several years old. There are a few complicating factors. I am trying to build it in a Parallels VM running Windows 11 on a Mac M1, so the architecture is ARM.

The game crashes while it’s doing the initial graphics setup:

            var fullScreen = true;

            graphics.SynchronizeWithVerticalRetrace = false; // graphics is the XNA Framework GraphicsDeviceManager
            device = graphics.GraphicsDevice;
            graphics.GraphicsProfile = GraphicsProfile.HiDef;
            
            Resolution.Init(ref graphics);
            var ActualResolution = GameHelper.BestResolution(GraphicsAdapter.DefaultAdapter.SupportedDisplayModes); // chooses the most appropriate resolution as a Vector2D
            var aspect = ActualResolution.X / ActualResolution.Y;
            maxX = width; // these are fields on the base game class
            maxY = (int)((float)width / aspect);
            Resolution.SetVirtualResolution(maxX, maxY);
            Resolution.SetResolution((int)ActualResolution.X, (int)ActualResolution.Y, fullScreen);
            VirtualToActualResolutionRatio = new Vector2(maxX / ActualResolution.X, maxY / ActualResolution.Y);
            graphics.PreferredBackBufferWidth = (int)ActualResolution.X;
            graphics.PreferredBackBufferHeight = (int)ActualResolution.Y;
            graphics.IsFullScreen = fullScreen;
            graphics.ApplyChanges();
            var pp = device.PresentationParameters;
            screenRenderTarget = new RenderTarget2D(device, pp.BackBufferWidth, pp.BackBufferHeight, false, device.DisplayMode.Format, DepthFormat.Depth24); // this is where I get the exception

Full stack trace:

   at Microsoft.Xna.Framework.Graphics.Texture2D.CreateTexture()
   at Microsoft.Xna.Framework.Graphics.RenderTarget2D.CreateTexture()
   at Microsoft.Xna.Framework.Graphics.RenderTarget2D.GenerateIfRequired()
   at Microsoft.Xna.Framework.Graphics.RenderTarget2D..ctor(GraphicsDevice graphicsDevice, Int32 width, Int32 height, Boolean mipMap, SurfaceFormat preferredFormat, DepthFormat preferredDepthFormat)
   at Magnetar.Magnetar.SetViewPortSize(Int32 width, Boolean reinit, GraphicsDeviceManager graphics) in \\Mac\Home\Documents\MonoMagnetar\MonoMagnetar\Magnetar.cs:line 2007
   at Magnetar.Magnetar.Initialize() in \\Mac\Home\Documents\MonoMagnetar\MonoMagnetar\Magnetar.cs:line 1678
   at Microsoft.Xna.Framework.Game.DoInitialize()
   at Microsoft.Xna.Framework.Game.Run(GameRunBehavior runBehavior)
   at Magnetar.Program.Main() in \\Mac\Home\Documents\MonoMagnetar\MonoMagnetar\Program.cs:line 20

Any suggestions?

i would get the mg source and trace it… I ddint get this exacgt erroro but I got a issue with creating Rendertarget from inside a DX project liked with shared code phojects taht caused conficts. i had a similar issue updating to MG 3.81 and netcore 6 wth shared content and assets in a game lib. my issue turned out to be using shared code… as in shlibs… but shared by both dx and Gl modules… what wujld happen is the core dlls are GL as a sort of proxy then the targets would be windows, or mac , or whaterver so DX and GL got mixed up. so in your VM situation without looking into the MG coded it could be really hard to figure otu even if it worked before. I didnt build and trace into the Monogame but i spend way too long strippping things trying to isolate the cause whch worked fine on Netstandard 2 by luck. so i think the direct path to see whats happening is because careful wth core dlls liked to Gl … and dont even use shared libs, and take the time to learn to build and trace in MG internals.

also if this still an issue try it on the ovveride for LoadContent… or on the first draw call. initialize override, is too early i think things aren’t set up enough… also you might try to build my sample, it makes a rendertarget and shader in a core dll…and waits till the first draw to make a rendertarget… hopefully it just builds and runs… it targets consoleDX , windowsDX, ive not tried on a mac specifically or VM…

GitHub - damian-666/MGShadersXPlatform: A bare bones, no repeat code, Monogame 3.8 , Net Core 6, with working pixel and other shaders that works in desktop GL and WindowsDX, and Android using shared linked content file, FX files, NetStandard core, and thinnest possible platform specific launchers