Search code examples
xamarin.formstargetinvocationexception

Xamarin Forms MessagingCenter: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation


Getting the below exception when trying to send a string value through MessagingCenter. The exception is on a PopupPage.

Exception Details:

exception:>>System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.NullReferenceException: Object reference not set to an instance of an object. at CatholicBrain.Views.BibleOrderGamePage.<.ctor>b__3_0 (CatholicBrain.Model.BibleOrderGameViewModel s, System.String answer) [0x00001] in F:\My Projects\Xamarin\catholicbrain-mobile-app\CatholicBrain\CatholicBrain\Views\BibleOrderGamePage.xaml.cs:30 at (wrapper managed-to-native) System.Reflection.RuntimeMethodInfo.InternalInvoke(System.Reflection.RuntimeMethodInfo,object,object[],System.Exception&) at System.Reflection.RuntimeMethodInfo.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x0006a] in <46c2fa109b574c7ea6739f9fe2350976>:0 --- End of inner exception stack trace --- at System.Reflection.RuntimeMethodInfo.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00086] in <46c2fa109b574c7ea6739f9fe2350976>:0 at System.Reflection.MethodBase.Invoke (System.Object obj, System.Object[] parameters) [0x00000] in <46c2fa109b574c7ea6739f9fe2350976>:0 at Xamarin.Forms.MessagingCenter+Subscription.InvokeCallback (System.Object sender, System.Object args) [0x00064] in D:\a\1\s\Xamarin.Forms.Core\MessagingCenter.cs:94 at Xamarin.Forms.MessagingCenter.InnerSend (System.String message, System.Type senderType, System.Type argType, System.Object sender, System.Object args) [0x0006b] in D:\a\1\s\Xamarin.Forms.Core\MessagingCenter.cs:217 at Xamarin.Forms.MessagingCenter.Xamarin.Forms.IMessagingCenter.Send[TSender,TArgs] (TSender sender, System.String message, TArgs args) [0x00013] in D:\a\1\s\Xamarin.Forms.Core\MessagingCenter.cs:115 at Xamarin.Forms.MessagingCenter.Send[TSender,TArgs] (TSender sender, System.String message, TArgs args) [0x00000] in D:\a\1\s\Xamarin.Forms.Core\MessagingCenter.cs:108 at CatholicBrain.Model.BibleOrderGameViewModel.StartBibleOrderCheck (CatholicBrain.Model.BibleOrderAnswer selectedItem, System.Collections.Generic.List`1[T] rightAnswerList) [0x00114] in F:\My Projects\Xamarin\catholicbrain-mobile-app\CatholicBrain\CatholicBrain\Model\BibleOrderGameViewModel.cs:195

MessagingCenter.Send

MessagingCenter.Send<BibleOrderGameViewModel, string>(this, "rightanswer", selectedItem.Answer);
await PopupNavigation.Instance.PopAsync();

MessagingCenter.Subscribe

MessagingCenter.Subscribe<BibleOrderGameViewModel, string>(this, "rightanswer", (s, answer) =>
{
    answerLabel.Text = answer;
});

Solution

  • I solved this issue by adding the MessagingCenter.Unsubscribe code in OnDisappearing() on the same page which contains the MessagingCenter.Subscribe code.

    protected override void OnDisappearing()
    {
        base.OnDisappearing();
        MessagingCenter.Unsubscribe<BibleOrderGameViewModel, string>(this, "rightanswer");
    }