0

I;m getting this error when I try to execute the code below, I'm trying to navigate to a new page and in that page I want the details that were on the previous page to display on the current page, however its giving me an error when I select anything on that page. Please kindly assist. Code: Application.Current.MainPage = new DistributorInfoPage(ProfileInfo.Name, ProfileInfo.Location, ProfileInfo.Image);

The full code:


       public void Profile_SelectionChanged(object sender, SelectionChangedEventArgs e)
       {

           var ProfileInfo = e.CurrentSelection as Profile;
           

           Application.Current.MainPage = new DistributorInfoPage(ProfileInfo.Name, ProfileInfo.Location, ProfileInfo.Image);
           ```

Lorry
  • 3
  • 3
  • 1
    Check that `ProfileInfo` actually exists; Split the call so that you can debug creation of `DistributorInfoPage` – ChrisBD Sep 09 '21 at 11:43
  • 2
    like the exception is telling you something is null. Either one of the arguments you are passing, or something in the constructor of `DistributorInfoPage`. The stack trace should show you exactly which line is causing it. Based on the single line of code you posted there is not much else we can do to help you. NullRefs are an extremely common issue in C# and you should learn how to track them down and deal with them. – Jason Sep 09 '21 at 11:43
  • I created profile info as a variable : public void Profile_SelectionChanged(object sender, SelectionChangedEventArgs e) { var ProfileInfo = e.CurrentSelection as Profile; Application.Current.MainPage = new DistributorInfoPage(ProfileInfo.Name, ProfileInfo.Location, ProfileInfo.Image); – Lorry Sep 09 '21 at 11:48
  • that doesn't mean that `ProfileInfo` can't be null if the cast fails. You should test for null before navigating – Jason Sep 09 '21 at 12:06
  • How do I test for null? Sorry I am still learning, I'm new. By testing you mean this is what I should do: if ( ProfileInfo != null) { – Lorry Sep 09 '21 at 12:34
  • Thank you I finally got it so what I did on the line I posted is add "?" between the variable ProfileInfo and Name as well as Location: ```Application.Current.MainPage = new DistributorInfoPage(ProfileInfo?.Name, ProfileInfo?.Location);``` – Lorry Sep 09 '21 at 13:31
  • For Anyone who might be experiencing the same problem I suggest this page: https://stackify.com/nullreferenceexception-object-reference-not-set/ . They explain everything very simply. – Lorry Sep 09 '21 at 13:34

0 Answers0