0

I'm new to Xamarin.Forms.

I got an app that works well on iOS. However, on Android, it crashes after some time, and throws the following error:

Unhandled Exception:

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

Debug says it happens on this code:

private void OnElementToggled(object sender, EventArgs e)
{
    this.Element.IsToggled = this.Control.Checked;
}

This toggle turns an option on or off inside our app. The toggle works fine on iOS. It also works on Android, but if I navigate around the app and switch the toggle on/off a few times, I get the error. I only get this error on Android, and only after I navigate around. Also, I get it at different times on simulator vs device (Galaxy S5 Neo). The simulator can run longer before I get the error.

I'm dumbfounded. How do I fix this?

I've searched and found What is a NullReferenceException, and how do I fix it?. That solution doesn't seem to apply in my case, because my code works fine on iOS and initially on Android.

Thank you very much for your time and help.

Etienne Juneau
  • 633
  • 5
  • 11
  • 1
    the simplest thing would be to add a try/catch around that code and handle that exception when it occurs. You could also add some logging to get a solid handle on how often it occurs, and under what circumstances – Jason Jul 04 '18 at 21:09
  • Hi Jason -- thx for your help. I'm so new, I don't know how to add a try/catch around that code and handle that exception when it occurs. Could you please recommend a good guide for that? – Etienne Juneau Jul 04 '18 at 21:16

1 Answers1

1

Try/Catch is a basic C# concept, any intro book will cover it

private void OnElementToggled(object sender, EventArgs e)
{
    try {
      this.Element.IsToggled = this.Control.Checked;
    catch (Exception ex) {
      // use logging (ie, appcenter.ms) to log this exception
    }
}
Jason
  • 86,222
  • 15
  • 131
  • 146
  • This helped me fix the crash issue. Thank you. Now, instead of crashing, using catch, I've told the app to show an error message. It doesn't crash, but I'm still getting the error. Can I ask what you'd do next to fix the error itself? – Etienne Juneau Jul 04 '18 at 23:28
  • 1
    as I suggested earlier, logging to determine how often this occurs might be a good step. I also have no idea what the context of your code is - I'd guess it's in a custom renderer - but that's just a guess. It could be some weird timing issue where an event is fired before a control is completely initialized. You could also approach it by just testing for null BEFORE doing any work, rather than catching the exception AFTER it happens. – Jason Jul 05 '18 at 00:45
  • You're exactly right: it's a custom renderer. If it's a timing issue, how do I find out? Zooming out, I wonder how you would go about debugging this bug, so I can find out what the root cause is -- timing issue or otherwise. Thanks! – Etienne Juneau Jul 05 '18 at 01:54
  • 1
    You just have to debug it. But frankly I wound't worry about it - add some defensive checks to prevent it from happening, and then forget about it unless it still creates an issue in your app – Jason Jul 05 '18 at 03:57