Exception Has Been Thrown by the target of an invocation only when running debug without VS

Hrcak94 5 Reputation points
2023-06-30T00:06:14.0533333+00:00

Hi there,

I've ported an app from Xamarin Forms to MAUI .NET 7
I've done that by creating a blank MAUI project, then importing all my classes.
For the pages, I created pages within the blank MAUI project, and then copied the XAML and page related code over to MAUI from XAMARIN project.

Then I've spent time fixing all the issues and porting things to MAUI.

The app works.

However, I'm plagued by this issue that is rather super annoying.

In Debug mode, if my phone is connected to VS, app works perfectly.
However, when I disconnect the app from the PC and try to restart the application, I get the following error when the user logs in and I call :

Application.Current.MainPage = new AppShell()


Exception has been thrown by the target of an invocation.
System.NullReferenceException: Object reference not set to an instance of an object.
   at Microsoft.Maui.Controls.Xaml.ApplyPropertiesVisitor.SetPropertyValue(Object xamlelement, XmlName propertyName, Object value, Object rootElement, INode node, HydrationContext context, IXmlLineInfo lineInfo)
   at Microsoft.Maui.Controls.Xaml.ApplyPropertiesVisitor.Visit(ValueNode node, INode parentNode)
   at Microsoft.Maui.Controls.Xaml.ValueNode.Accept(IXamlNodeVisitor visitor, INode parentNode)
   at Microsoft.Maui.Controls.Xaml.ElementNode.Accept(IXamlNodeVisitor visitor, INode parentNode)
   at Microsoft.Maui.Controls.Xaml.ElementNode.Accept(IXamlNodeVisitor visitor, INode parentNode)
   at Microsoft.Maui.Controls.Xaml.ElementNode.Accept(IXamlNodeVisitor visitor, INode parentNode)
   at Microsoft.Maui.Controls.Xaml.RootNode.Accept(IXamlNodeVisitor visitor, INode parentNode)
   at Microsoft.Maui.Controls.Xaml.XamlLoader.Visit(RootNode rootnode, HydrationContext visitorContext, Boolean useDesignProperties)
   at Microsoft.Maui.Controls.Xaml.XamlLoader.Load(Object view, String xaml, Assembly rootAssembly, Boolean useDesignProperties)
   at Microsoft.Maui.Controls.Xaml.XamlLoader.Load(Object view, String xaml, Boolean useDesignProperties) 
   at Microsoft. Maui.Controls.Xaml. XamlLoader.Load(Object view, Type callingType) at Microsoft. Maui.Controls.Xaml.Exte nsions.LoadFromXaml[Inventures Page] (InventuresPage view, Type callingType) at Article Tracker.Views. MainPages.PageInvent ures.InventuresPage.InitializeComponent() at Views. Main Pages.Pagelnv entures.InventuresPage..ctor() at System.Reflection.ConstructorInvoker .InterpretedInvoke(Object obj, Span`1 args, BindingFlags invokeAttr)

If I run app in Release mode, it works.
If I disconnect my phone from VS and run the release mode build, it works.

It only doesn't work in Debug mode when it's not connected to Visual Studio.
And I still haven't tested apk build on release.

Any ideas why this might be happening?
As I've ported the code from Xamarin I can't really pinpoint it to anything specific and considering that I can't even run debugger in Visual Studio to see what might be wrong, I'm clueless...

P.S. I'm using TabBar in AppShell.

Thanks.

Developer technologies | .NET | .NET MAUI
{count} votes

1 answer

Sort by: Most helpful
  1. Hrcak94 5 Reputation points
    2023-07-06T17:58:21.7933333+00:00

    The issue to my problem was an event handler that was leftover from Xamarin Forms. It had incorrect parameter. There was no report of issue in Visual Studio and since it was empty, I paid no attention to it, especially because the application worked while debugging with cable and Visual Studio.

    I had to use logcat to analyze the crash and then figured out that the event handler was causing it, then I analyzed thee code and I saw that I had two event handlers, one was from the XF. Removing the handler resolved the crashes in both Release and Debug on Android builds.

    1 person found this answer helpful.
    0 comments No comments

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.