NullReferenceException when creating instance of G

2019-07-18 21:32发布

问题:

I just installed xamarin and monogame on OSX I have tried to run a really basic demo project that works perfectly on windows. But I get the following error:

System.NullReferenceException: Object reference not set to an instance of an object
  at Microsoft.Xna.Framework.OpenTKGameWindow.Initialize () [0x00000] in <filename unknown>:0
  at Microsoft.Xna.Framework.OpenTKGameWindow..ctor () [0x00000] in <filename unknown>:0
  at Microsoft.Xna.Framework.OpenTKGamePlatform..ctor (Microsoft.Xna.Framework.Game game) [0x00000] in <filename unknown>:0
  at Microsoft.Xna.Framework.GamePlatform.Create (Microsoft.Xna.Framework.Game game) [0x00000] in <filename unknown>:0
  at Microsoft.Xna.Framework.Game..ctor () [0x00000] in <filename unknown>:0
  at test.Game1..ctor () [0x00057] in /Users/remojansen/Desktop/test/test/Game1.cs:37
  at test.Program.Main () [0x00001] in /Users/remojansen/Desktop/test/test/Program.cs:19

There is nothing wrong about the code so I must be missing a DLL or something? Do you have any ideas about what could be missing?

Thanks :)

[EDIT]

The disassembly shows a error in the second line:

00000155   callvirt IWindowInfo OpenTK.NativeWindow:get_WindowInfo ()
0000015a   ldnull 
0000015b   callvirt Object System.Reflection.PropertyInfo:GetValue (Object, Object[])
00000160   unbox.any System.IntPtr
00000165   stfld 
0000016a   call GraphicsMode OpenTK.Graphics.GraphicsMode:get_Default ()
0000016f   ldarg.0 
00000170   ldfld 
00000175   callvirt IWindowInfo OpenTK.NativeWindow:get_WindowInfo ()
0000017a   newobj Void OpenTK.Graphics.GraphicsContext:.ctor (GraphicsMode, IWindowInfo)

I have also manually compiled OpenTK and when I run the examples they seem to work fine:

I guess the problem is in monogame, I installed monogame using the xamarin monogame project template.

回答1:

Xamarin picked up the wrong Monogame.Framework.dll. It's trying to load the WindowsGL or Linux project (which uses OpenTKGamePlatform -- for MacOS you want MacGamePlatform in that stack trace).

The MonoGame Xamarin template has a lot of problems. One of them is it will always pick the wrong framework. You need to manually browse to the framework dll in /Users//Library/Application Support/XamarinStudio-4.0/LocalInstall/Addins/MonoDevelop.MonoGame.3.0.1/assemblies/MacOS

However, there's still problems with that if you're using the 3.0.1 release -- there's a MonoMac.dll API conflict. I strongly recommend downloading the latest MonoGame source code, and building the MonoGame.Framework.MacOS project yourself, and referencing the framework assembly there. You can still use the Xamarin template you installed, just change the assembly reference. You might also need to set your logo.png build action to 'Content' to fully get going.



回答2:

For anyone who may stumble upon this in the future, I was having the same problem on windows. Xamarin seemed to be picking the wrong MonoGame.Framework.dll even though I built the github version myself and set the reference manually.

I got lucky and right clicked the MonoGame.Framework reference inside my projects reference folder and un-checked the checkbox that said "Local Copy"; This solved the problem for me!!

I unchecked this checkmark

Good luck!