1

I have a Xamarin.Forms app that uses push notifications. For some reason, the following line:

(App.Current.MainPage as MainPage)?.AddMessage(body);

that is called from Android native OnMessageReceived(), throws NullReferenceException.

Why can this happen? Isn't App.Current to be accessible from the platform-specific project?

Edit:

Here is the full OnMessageReceived() code:

    public override void OnMessageReceived(RemoteMessage message)
    {
        base.OnMessageReceived(message);
        string messageBody;

        if (message.GetNotification() != null)
        {
            messageBody = message.GetNotification().Body;
        }

        // NOTE: test messages sent via the Azure portal will be received here
        else
        {
            messageBody = message.Data.Values.First();
        }

        // convert the incoming message to a local notification
        SendLocalNotification(messageBody);

        // send the incoming message directly to the MainPage
        SendMessageToMainPage(messageBody);
    }
16
  • 1
    I think it matters if it is called before or after LoadApplication(). Commented Dec 3, 2020 at 19:24
  • @Cfun As the app was running on the background, I believe the LoadApplication() had already been called? Commented Dec 3, 2020 at 20:36
  • I think so, could you confirm exactly by debugging at that point which object is null? App.Current or App.Current.MainPage ? Commented Dec 3, 2020 at 20:49
  • @Cfun The problem is that I cannot debug it, as (according to the documentation) emulator cannot be configured to receive notifications. I believe only App.Current can be null, as I use ?. after App.Current.MainPage. Commented Dec 3, 2020 at 20:59
  • oh good to know about emulator, Have you tried with (App.Current?.MainPage as MainPage)?.AddMessage(body); ? Commented Dec 3, 2020 at 21:06

1 Answer 1

2

App.Current.MainPage may contain any Page, not necessary your type MainPage, for example it could be NavigationPage. As the result of casting is null it is clear that it is of some other type.

Also it could happen that nothing is assigned to it.

Sign up to request clarification or add additional context in comments.

11 Comments

But I do know that it is MainPage. Page page = FreshPageModelResolver.ResolvePageModel<MainPageModel>(); FreshNavigationContainer simpleNav = new FreshNavigationContainer(page); MainPage = simpleNav;
I really don't know how Fresh works (and don't want to know), but I can tell you 100% that it is not. Why don't you debug and check it. You will see that it is not as simply it can't be if you see the error.
interesting about fact about as good to know.
@DavidShochet if App.Current is null, add some timer, possibly even Task.Delay will work. Probably sometimes it needs some time to initialize.
@DavidShochet Generally there are two possible cases - either we don’t know something or we have some wrong assumptions. There is the third one - black magic, but this far it has never happened.
|

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.