22

I developed a game for Android using MonoGame & Xamarin. I incorporated BugSense into it and quickly started getting the following exception stack trace:

System.NullReferenceException: Object reference not set to an instance of an object
at Microsoft.Xna.Framework.AndroidGameWindow.SetDisplayOrientation (Microsoft.Xna.Framework.DisplayOrientation) <0x001c4>
at Microsoft.Xna.Framework.AndroidGameWindow.SetOrientation (Microsoft.Xna.Framework.DisplayOrientation,bool) <0x00097>
at Microsoft.Xna.Framework.OrientationListener.OnOrientationChanged (int) <0x001c7>
at Android.Views.OrientationEventListener.n_OnOrientationChanged_I (intptr,intptr,int) <0x0003f>
at (wrapper dynamic-method) object.ed9d7c7c-f3e6-4d7a-9249-1a139a251aed (intptr,intptr,int) <0x00043>

This is how my activity is setup:

[Activity(Label = "My Cool Game"
        , MainLauncher = true
        , Icon = "@drawable/icon"
        , Theme = "@style/Theme.Splash"
        , AlwaysRetainTaskState = true
        , LaunchMode = Android.Content.PM.LaunchMode.SingleTask
        , ScreenOrientation = ScreenOrientation.Portrait
        , ConfigurationChanges = ConfigChanges.Orientation | ConfigChanges.Keyboard | ConfigChanges.KeyboardHidden)]

And in the game constructor I have the following:

public Game1()
{
    _graphics = new GraphicsDeviceManager(this);
    _graphics.PreferredBackBufferFormat = SurfaceFormat.Color;
    _graphics.IsFullScreen = true;
    _graphics.SupportedOrientations = DisplayOrientation.Portrait | DisplayOrientation.PortraitDown;
    _graphics.ApplyChanges();
}

The project is set to compile against v2.3. I haven't had any issues relating to an orientation on my test devices so I am not sure what is causing this exception, hence I cant fix it.

Fred
  • 3,365
  • 4
  • 36
  • 57
user123
  • 632
  • 1
  • 6
  • 22
  • 4
    I'm pretty sure _graphics won't be initialized in the Game1's constructor. You'll probably need to move the code into Game1's Initialize method. – craftworkgames Sep 04 '14 at 03:40
  • 1
    @craftworkgames I have edited the constructor code, I originally didnt include all the graphics lines. If that doesn't matter however, are you saying that this could be the cause of the exception? if so, how to be sure before I push an update to the app? – user123 Sep 04 '14 at 05:07
  • 3
    Actually, my last comment was from my (apparently bad) memory. I've just checked my code and it turns out I was wrong. The _graphics field is certainly okay to be initialized in the constructor. I see you have updated the code in the question, it looks fine to me. Sorry about the misleading comment before. – craftworkgames Sep 04 '14 at 10:37
  • 1
    Are you getting additional information on the device/platform for which these exceptions are occurring? Meaning the type of device and the version of the Android OS that it is running? – jensendp Sep 09 '14 at 21:14
  • 1
    @jensendp its occurring on devices running 4.1.2 and above. I also just noticed that BugSense reports these exceptions as being handled exceptions. – user123 Sep 10 '14 at 01:11
  • 1
    Did you use any test devices that are at 4.1.2 or above? – jensendp Sep 10 '14 at 01:26
  • 1
    @jensendp yup and I didnt face any problems, as in crashes, but I just learned that this exception is of the handled kind so I would not have detected it. – user123 Sep 10 '14 at 01:58
  • 1
    Gotcha. Well that is a little better news. – jensendp Sep 10 '14 at 02:07
  • 1
    NullReferenceException's are never really a good thing even if they are "handled" because they imply that the programmer thought something wouldn't be null and it was. This could cause unexpected behavior because code may not have executed after the exception. In reality though, it's probably not a real problem in the real world so I'd just log it as an issue on the MonoGame github page and move on. – craftworkgames Sep 12 '14 at 03:53
  • 1
    For anyone interested, this looks like the method in the exception stack trace. https://github.com/mono/MonoGame/blob/develop/MonoGame.Framework/Android/AndroidGameWindow.cs#L228 – craftworkgames Sep 12 '14 at 03:56

1 Answers1

1

I have come across this Null Reference exception before working on a different project. You will have to pardon my ignorance of your particular field in gaming but let me explain to you what this means perhaps it will help you solve this problem. What this exception means is that there is no object of a particular sort available in memory for the system to be able to work on it. An instance of this object must be created with the new keyword or in other ways for it to be recognised as a valid object in memory.

From reading the above error message it seems to want an object to initalise the display orientation. No where in your constructor or activity setup do I see anything creating any object for display orientation. Thus the object is not there in memory so it is null.

Soliman Soliman
  • 159
  • 1
  • 4
  • 17