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.

2
  • 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 Commented Jul 4, 2018 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? Commented Jul 4, 2018 at 21:16

1 Answer 1

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
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

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?
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.
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!
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.