0

I have a ContentPage in Xamarin that I load from another page, with arguments.

await Navigation.PushAsync(new AnnouncementPage(verificationToken, sessionID, authenticationToken));

And here is AnnouncementPage.xaml.cs:

public partial class AnnouncementPage : ContentPage 
{
        private string _verificationToken;
        private string _sessionID; 
        private string _authenticationToken;

        public AnnouncementPage()
        {
            InitializeComponent();
        }

        public AnnouncementPage(string verificationToken, string sessionID, string authenticationToken)
        {
            BindingContext = new AnnouncementViewModel();
            _verificationToken = verificationToken;
            _sessionID = sessionID;
            _authenticationToken = authenticationToken;
            GetAnnouncements();
            InitializeComponent();
        }

        // Other code

        private void Announcement_Tapped(object sender, EventArgs e)
        {
            Console.WriteLine(_verificationToken.ToString());
        }
}

Why does Announcement_Tapped throw a NullReferenceException? I am sure that _verificationToken is not null (at least in other parts of the code) as I use _verificationToken in GetAnnouncements(). I have searched everywhere about this issue and their code structure is the same as mine. My only guess is that this has to do with the AnnouncementPage() default constructor having no arguments. Any help would be appreciated.

UnidentifiedX
  • 189
  • 1
  • 8
  • 1
    "My only guess is that this has to do with the AnnouncementPage() default constructor" well yeah, if that runs, `_verificationToken` is null. – gunr2171 Jul 15 '22 at 12:05
  • 2
    Also, why are you calling `ToString` on `_verificationToken` when it is already a string? – DavidG Jul 15 '22 at 12:07
  • string type has a default value of null. So, you could change string _verificationToken; to `string _verificationToken = string.Empty;` Additionally, you could remove the first AnnouncementPage() constructor and add `BindingContext = new AnnouncementViewModel();` to your other constructor. Additionally, Xamarin can be super buggy. Sometimes tapped, item changed and focus events trigger unexpectedly; e.g. on page appearing. – chri3g91 Jul 15 '22 at 12:08
  • "I am sure that _verificationToken is not null" - Check, don't be sure. And I mean in the `Announcement_Tapped` method. – M Kloster Jul 15 '22 at 12:09
  • @gunr2171 but when will the announcementpage default constructor run? – UnidentifiedX Jul 15 '22 at 12:26
  • @DavidG to just throw an error – UnidentifiedX Jul 15 '22 at 12:28
  • @MKloster of course _verificationToken would be null in Announcement_Tapped, that's why I'm asking the question. It is not null in other places I use it though. – UnidentifiedX Jul 15 '22 at 12:29
  • You don’t need the dEfault constructor in this case. But you are not setting BindingContext in the other constructor, which is odd. It’s also good practice to call InitializeComponent first. And in you make _verificationToken a property you can add a breakpoint in the setter to validate that it’s value is being set as expected – Jason Jul 15 '22 at 13:22

0 Answers0