System.NullReferenceException: Object reference not set to an instance of an object

I am getting quite a few bug reports from BugSense that go as follows:

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>

The app runs just fine on my 2 Android devices and I had never seen an issue with device orientation. I have my activity setup as follows:

[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)]

Does your app only work in Portrait mode? If it does it might be good to add this on your Game constructor:

public Game1()
{
    // Add this line
    graphics.SupportedOrientations = DisplayOrientation.Portrait;
}

Yes its only Portrait. I already have this in my Game constructor:

_graphics.SupportedOrientations = DisplayOrientation.Portrait | DisplayOrientation.PortraitDown;

I noticed that activity’s ScreenOrientation has many more portrait values other than ScreenOrientation.Portrait, like reversePortrait & sensorPortrait. Maybe I should add those since I set _graphics to both Portrait & PortraitDown.

Well it seems that the activity’s ScreenOrientation enum doesn’t support multiple values. Would setting it to SensorPortrait instead of just Portrait solve this issue? or maybe _graphics.SupportedOrientations shouldnt be set to both DisplayOrientation.Portrait & DisplayOrientation.PortraitDown? I still dont know whats the cause of the problem to begin with…

I am getting tons of these errors reported by BugSense everyday, I don’t want to push an update unless I know I fixed the issue. Problem is I cant replicate it. Any ideas to what the cause is?

Reviving an old thread here. But I am getting this issue recently as well. I believe this is starting when I added implementation to have a splash screen in either portrait or landscape mode: https://forums.xamarin.com/discussion/comment/324214/#Comment_324214

I have the supported orientations in LoadContent()
Graphics.SupportedOrientations = DisplayOrientation.Portrait |
DisplayOrientation.PortraitDown |
DisplayOrientation.LandscapeLeft |
DisplayOrientation.LandscapeRight;

The error is an object reference not set error. Here is the stack trace. Any help would be appreciated.

StackTrace: at Microsoft.Xna.Framework.OrientationListener.OnOrientationChanged (System.Int32 orientation) [0x00019] in <8335ffb25c73411d8c018e841c87a942>:0
at Android.Views.OrientationEventListener.n_OnOrientationChanged_I (System.IntPtr jnienv, System.IntPtr native__this, System.Int32 orientation) [0x00009] in <53c60d650862457f9c9b6a8ddcee80ca>:0
at (wrapper dynamic-method) System.Object.13(intptr,intptr,int)

SupportedOrientations used to work in MG 3.5 but from v3.6 the functionality was removed.

That is good to know, Thanks. In the activity class I also have the following:

ScreenOrientation = ScreenOrientation.FullUser

Any ideas what may be going on here or how I can fix it?

I don’t know how others manage to work with this, I guess you can lock the ScreenOrientation to either Landscape or Portait in activity1.cs and that’s it.

I had an old project on XNA/WindowsPhone and MG/Android 3.4 and when I upgraded to 3.7 I didn’t want to add extra platform specific workarounds since I knew that the code already worked.
Instead I had the original functionally restored for my MG branch. Additionally I added a small delay to make rotation shock-proof and reduce the effective angle from 90 to 45 degrees (with a 45 degree ‘no-change’ gap between orientations).

If you want to use my changes you can find the binaries in my nuGet here.
or apply this commit in your branch.
You also have to change ScreenOrientation = ScreenOrientation.Landscape in activity1.cs.

It appears that the null reference error is not happening on

Microsoft.Xna.Framework.AndroidGameWindow.SetDisplayOrientation (as the previous post suggested)

but rather

Microsoft.Xna.Framework.OrientationListener.OnOrientationChanged

Based on the code (OnOrientationChanged), this happens before SetDisplayOrientation. I would almost venture to say that users are rotating the screen prior to the Game Window being initialized…