Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

System.NullReferenceException when resuming app in Android 5.1

Hi all,

I'm currently pulling my hair out over an error occurring in my Xamarin.Forms App. When I leave the app (hit the home button), and re-enter I am getting a "System.NullReferenceException: Object reference not set to an instance of an object" error. I'm tracing it through the OnResume function in app.xaml.cs where I'm opening a new navigation page (the exact same thing happens OnStart). It then opens the new page and seems to get stuck after the "InitializeComponent" function where the "System.NullReferenceException" is thrown.

It only seems to be happening when I test it on my Android device running Android 5.1, it won't happen in my iOS simulator running iOS 9.3 or my other Android device running Android 6.0.1. I have all the correct Android SDKs installed and up to date and it otherwise runs without error on Android 5.1.

I'm using Xamarin.Forms 2.3.1.114.

Let me know if you need any further information and sorry in advance, I'm new to Xamarin.
Any ideas are appreciated, thanks in advance!

In my App.xaml.cs:
`
protected override void OnStart()
{
base.OnResume();

MainPage = new NavigationPage(new MyProject.Views.MyPage());

}

protected override void OnResume()
{
base.OnResume();

MainPage = new NavigationPage(new MyProject.Views.MyPage());

}

`

Best Answer

Answers

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    Is Visual Studio breaking on the line throwing the error?
    If not open the Exception Settings and tick the checkbox for System.NullReferenceException. (Its part of the second group "Common Language Runtime Exceptions")
    That will help you narrow it to the line throwing the error.
    After that, this tutorial should help with further debugging

  • MarkMcKinneyMarkMcKinney USMember

    Hi Clint,

    Thanks for the help! It is not in fact breaking on the line that was throwing this error, it appears to be . I did as you suggested and ticked the box for "System.NullReferenceException" but I still couldn't get it to break on the specific line that is causing the issue and no disassembly was apparently available for viewing. So I went ahead and checked the logcat (my apologies for not doing that to begin with) and found this error.

    UNHANDLED EXCEPTION:
    System.NullReferenceException: Object reference not set to an instance of an object.
    at Xamarin.Forms.Platform.Android.AppCompat.Platform.Xamarin.Forms.Platform.Android.IPlatformLayout.OnLayout (Boolean changed, Int32 l, Int32 t, Int32 r, Int32 b) [0x00017] in C:\BuildAgent\work\aad494dc9bc9783\Xamarin.Forms.Platform.Android\AppCompat\Platform.cs:198
    at Xamarin.Forms.Platform.Android.PlatformRenderer.OnLayout (Boolean changed, Int32 l, Int32 t, Int32 r, Int32 b) [0x0000e] in C:\BuildAgent\work\aad494dc9bc9783\Xamarin.Forms.Platform.Android\PlatformRenderer.cs:73
    at Android.Views.ViewGroup.n_OnLayout_ZIIII (IntPtr jnienv, IntPtr native__this, Boolean changed, Int32 l, Int32 t, Int32 r, Int32 b) [0x00009] in /Users/builder/data/lanes/3540/1cf254db/source/monodroid/src/Mono.Android/platforms/android-23/src/generated/Android.Views.ViewGroup.cs:3427
    at (wrapper dynamic-method) System.Object:488f1413-f2c9-4fb6-81ea-8ba5cbb2006a (intptr,intptr,bool,int,int,int,int)

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    Sounds like one of those pain in the arse bugs that takes hours of trudging through code and guessing to find. I hate those, but we all get them from time to time.

    So its doing it at "OnLayout"... But not hitting a line of C# when it does it.
    So maybe a XAML binding issue? Binding to an object or referencing a property on an object but that object is null. Setting the binding context to a null object?
    For example: You have a binding to show the UserName on the CurrentUser... Only nobody has logged in yet so there is no CurrentUser, thus null reference when trying to drill down to the CurrentUser.UserName property.

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    Attached is a screen shot of where to tick that settings, for those that might need it.

Sign In or Register to comment.