0

I have a .NET MAUI project and just want to display an alert. This already worked well, but in some cases it throws me a NullRefereceException. The Code is:

private async void Info_Appearing(object sender, EventArgs e)
{
    await Application.Current.MainPage.DisplayAlert("Title", "Text", "OK");
}

I totally have no clue, what the problem is. The stack trace is: enter image description here

It seems it's a problem of the Android debugger/emulator, because with my local Android device, everything works fine...

Thank you for helping :)

nicetomitja
  • 145
  • 10
  • My bet is Application.Current==null and you are calling this earlier than that was populated – Nick Kovalsky Nov 14 '22 at 17:47
  • Whenever you get an exception you don't understand, wrap the code in `try .. catch`. Running under debugger, this should make it easier to get a *useful* stack trace, from the exception in the catch block. Presumably, either `Application.Current` or `Application.Current.MainPage` are still null. Which you could also see by adding some test code before that line: `if (Application.Current == null) {}; if (Application.Current.MainPage == null) {};` With appropriate newlines, so you can put breakpoints on both `{}` lines. – ToolmakerSteve Nov 14 '22 at 20:03
  • If only have this problem with the Android emulator. Everything works fine on the local android device. I tried to wrap this line with a try-catch, but the debugger will not execute the catch area, the whole application crashes before. I also added a check, if Application.Current or Application.Current.MainPage is null. Neither is in emulator. – nicetomitja Nov 15 '22 at 09:08

1 Answers1

0

Make sure that you have set the MainPage in constructor before it is assigned to the MainPage property of your App. Otherwise, when using the App.Current.MainPage before then, it might throw a NullReferenceException.

As an alternative workaround, you can refer to the snippets below:

      public static Page rootPage { get; set; } 
      public App()
      {
            InitializeComponent();

            MainPage = new MainPage();
            App.rootPage = MainPage;
      }

private async void Button_Clicked(object sender, EventArgs e) 
{
     await App.rootPage.DisplayAlert("Title", "Text", "OK");
}
Alexandar May - MSFT
  • 6,536
  • 1
  • 8
  • 15
  • I also tried this. But unfortunately without any success. I get the same result - the whole application crashes. No chance for catching the exception... – nicetomitja Nov 15 '22 at 09:11
  • What if you use `await DisplayAlert("Title", "Text", "OK");` directly, will it throw a NullRefereceException? – Alexandar May - MSFT Nov 16 '22 at 07:10
  • Then i get error "System.Reflection.TargetInvocationException: 'Exception has been thrown by the target of an invocation.'" – nicetomitja Nov 16 '22 at 12:39
  • You may need to warp the alert in `await Task.Run(async() => { Device.BeginInvokeOnMainThread(async () => { await DisplayAlert("Title", "Text", "OK"); }); });` from the main thread. BTW, what the API version of your Android Emulator? – Alexandar May - MSFT Nov 18 '22 at 07:09