Error using GoToAsync

Jassim Al Rahma 1,571 Reputation points
2022-12-14T20:05:44.46+00:00

Hi,

I am trying to show my ContentPage in MAUI but getting below error:

System.NullReferenceException: Object reference not set to an instance of an object.
at xxx.Mobile.OnBoarding.MyCountryTapGestureRecognizer_Tapped(Object sender, TappedEventArgs e) in /Users/jassim/Projects/xxx/xxx/Mobile/OnBoarding.xaml.cs:129
at at Microsoft.Maui.Controls.TapGestureRecognizer.SendTapped(View sender, Func2 getPosition) at at Microsoft.Maui.Controls.Platform.GestureManager.ProcessRecognizerHandlerTap(WeakReference weakEventTracker, WeakReference weakRecognizer, CGPoint originPoint, Int32 uiTapGestureRecognizerNumberOfTapsRequired, UITapGestureRecognizer uITapGestureRecognizer) at at Microsoft.Maui.Controls.Platform.GestureManager.<>c__DisplayClass24_0.<CreateTapRecognizer>b__0(UITapGestureRecognizer sender) at at UIKit.UIGestureRecognizer.Callback1[[UIKit.UITapGestureRecognizer, Microsoft.iOS, Version=16.0.700.0, Culture=neutral, PublicKeyToken=84e04ff9cfb79065]].Activated(UITapGestureRecognizer sender)
at at UIKit.UIApplication.UIApplicationMain(Int32 argc, String[] argv, IntPtr principalClassName, IntPtr delegateClassName)
at at UIKit.UIApplication.Main(String[] args, Type principalClass, Type delegateClass)
at xxx.Program.Main(String[] args) in /Users/jassim/Projects/xxx/xxx/Platforms/iOS/Program.cs:13

In my AppShell:

namespace xxx.Mobile;  
  
public partial class AppShell : Shell  
{  
	public AppShell()  
	{  
		InitializeComponent();  
  
        Routing.RegisterRoute(nameof(Mobile.Country), typeof(Mobile.Country));  
    }  
}  

and then:

async void MyCountryTapGestureRecognizer_Tapped(System.Object sender, Microsoft.Maui.Controls.TappedEventArgs e)  
{  
    await Shell.Current.GoToAsync($"Mobile.Country");  
}  

Kindly help...

Thanks.
Jassim

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,487 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 42,431 Reputation points Microsoft Vendor
    2022-12-15T05:19:03.283+00:00

    Hello,

    I noticed that you used nameof(Mobile.Country) to register the shell route, and called it by Shell.Current.GoToAsync($"Mobile.Country").

    As a matter of fact, when you use nameof(Mobile.Country), the value of it would be the class name Country, not Mobile.Country.

    Therefore, it should be the cause of your issue.

    You could use Shell.Current.GoToAsync($"{nameof(Mobile.Country)}") to execute the navigation event.

    Best Regards,

    Alec Liu.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.