4

I am writing my own game engine in XNA and started to port it into MonoGame so that I can put it on Android/iOS/Windows 8. For some reason I am getting a null reference exception when the main create a new game object. So the code that allocates the object is:

    static void Main(string[] args)
    {
        //game g = new game();
        using (game game = new game())
        {
            game.Run();
        }
    }

and the error is as

    public lesaEngine()

which is the base constructor for the game object.

the inheritance is just as always

    class lesaEngine : Microsoft.Xna.Framework.Game
    class game : lesaEngine

Not sure whats going on here. It works normally under normal XNA. I am using Visual Studio 2012 for the port.

phoog
  • 42,068
  • 6
  • 79
  • 117
Serguei Fedorov
  • 7,763
  • 9
  • 63
  • 94
  • Is the NRE occurring at the `game.Run()` call, or is it being thrown by the constructor called in the `new game()` expression? If the error is being thrown in the `lesaEngine()` constructor, can you post the body of that constructor? – phoog Nov 28 '12 at 23:32
  • In addition to what @phoog has said, constructors are not inherited so your game constructor must call the lesaEngine constructor, if needed. – Chris Dunaway Nov 28 '12 at 23:37
  • 1
    @ChrisDunaway if a derived class's constructor doesn't explicitly call another constructor, then it will implicitly call the base class's default constructor. If there's no default constructor in the base class, or if it is not accessible, the code will not compile. – phoog Nov 28 '12 at 23:41
  • To rephrase my first comment: can you post the stack trace of the NullReferenceException? – phoog Nov 29 '12 at 01:55
  • So just to clarify, the above code compiles and runs under XNA on Windows? But now you are compiling and running that same code under what? iOS, Android, Windows 8? – Dominique Dec 04 '12 at 22:53
  • I am using Windows 8. It is a MonoGame project. It runs and compiles just fine under the normal Windows Environment. – Serguei Fedorov Dec 05 '12 at 02:47
  • Did you create a new Win8 monogame project? if so the entry point should look more like this #if !NETFX_CORE using (MyGame game = new MyGame()) { game.Run(); } #endif #if NETFX_CORE var factory = new MonoGame.Framework.GameFrameworkViewSource(); Windows.ApplicationModel.Core.CoreApplication.Run(factory); #endif } – roundcrisis Mar 18 '13 at 22:30
  • 1
    @Miau that looks like an answer. Why don't you put it in the answer box? – kristianp Jul 04 '13 at 22:38
  • I thought that maybe it was too obvious but ... yes I guess – roundcrisis Jul 05 '13 at 15:01

1 Answers1

1

Did you create a new Win8 monogame project? if so the entry point should look more like this

#if !NETFX_CORE 
  using (MyGame game = new MyGame()) 
  { 
      game.Run();
  } 
#endif 
#if NETFX_CORE 
  var factory = newMonoGame.Framework.GameFrameworkViewSource<MyGame>();
  Windows.ApplicationModel.Core.CoreApplication.Run(factory); 
#endif } 
roundcrisis
  • 17,276
  • 14
  • 60
  • 92