xamarin / Xamarin.Forms

Xamarin.Forms is no longer supported. Migrate your apps to .NET MAUI.
https://aka.ms/xamarin-upgrade
Other
5.64k stars 1.88k forks source link

[Bug] NullReferenceException thrown when loading specific page #15471

Closed BlyZeYT closed 2 years ago

BlyZeYT commented 2 years ago

Description

That's the exception: {System.NullReferenceException: Object reference not set to an instance of an object. at Android.Runtime.JNINativeWrapper._unhandled_exception (System.Exception e) [0x0000e] in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.Runtime/JNINativeWrapper.g.cs:12 at Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PPL_V (_JniMarshal_PPL_V callback, System.IntPtr jnienv, System.IntPtr klazz, System.IntPtr p0) [0x0001d] in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.Runtime/JNINativeWrapper.g.cs:111 at (wrapper native-to-managed) Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PPL_V(intptr,intptr,intptr)}

That's the stack trace of the exception: " at Android.Runtime.JNINativeWrapper._unhandled_exception (System.Exception e) [0x0000e] in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.Runtime/JNINativeWrapper.g.cs:12 \n at Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PPL_V (_JniMarshal_PPL_V callback, System.IntPtr jnienv, System.IntPtr klazz, System.IntPtr p0) [0x0001d] in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.Runtime/JNINativeWrapper.g.cs:111 \n at (wrapper native-to-managed) Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PPL_V(intptr,intptr,intptr)"

Exception source: "Mono.Android"

Thats my xaml file:

<?xml version="1.0" encoding="utf-8" ?>
<FlyoutPage xmlns="http://xamarin.com/schemas/2014/forms"
            xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
            xmlns:local="clr-namespace:QRCodeScanner"
            x:Class="QRCodeScanner.Generator"
            Title="FlyoutPage"
            BackgroundColor="#2d2d2d">

    <FlyoutPage.Flyout>
        <local:FlyoutMenuPage x:Name="flyout"/>
    </FlyoutPage.Flyout>

    <FlyoutPage.Detail>
        <NavigationPage>
            <x:Arguments>
                <local:History/>
            </x:Arguments>
        </NavigationPage>
    </FlyoutPage.Detail>
</FlyoutPage>

Thats my .cs file:

namespace QRCodeScanner
{
    using System;

    using Xamarin.Forms;
    using Xamarin.Forms.Xaml;

    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class Generator : FlyoutPage
    {
        public Generator()
        {
            InitializeComponent();
            flyout.qrcodeMenuList.ItemSelected += OnSelectedItem;
        }

        private void OnSelectedItem(object sender, SelectedItemChangedEventArgs e)
        {
            var item = e.SelectedItem as FlyoutItemPage;
            if (item is null) return;

            Detail = new NavigationPage((Page)Activator.CreateInstance(item.TargetPage));
            flyout.qrcodeMenuList.SelectedItem = null;
            IsPresented = false;
        }
    }
}

See screenshot.

Steps to Reproduce

  1. I don't know, it happened out of nowhere

Expected Behavior

The App should start normally

Actual Behavior

The app crashes everytime with a NullReferenceException

Basic Information

Environment

Show/Hide Visual Studio info ``` Microsoft Visual Studio Community 2022 Version 17.2.6 VisualStudio.17.Release/17.2.6+32630.192 Microsoft .NET Framework Version 4.8.04161 Installierte Version: Community Visual C++ 2022 00482-90000-00000-AA081 Microsoft Visual C++ 2022 .NET Core-Debugging mit WSL 1.0 .NET Core-Debugging mit WSL ADL Tools Service Provider 1.0 This package contains services used by Data Lake tools ASA Service Provider 1.0 ASP.NET and Web Tools 2019 17.2.393.26812 ASP.NET and Web Tools 2019 Azure App Service-Tools v3.0.0 17.2.393.26812 Azure App Service-Tools v3.0.0 Azure Data Lake Tools for Visual Studio 2.6.5000.0 Microsoft Azure Data Lake Tools for Visual Studio Azure Functions and Web Jobs Tools 17.2.393.26812 Azure Functions and Web Jobs Tools Azure Stream Analytics Tools for Visual Studio 2.6.5000.0 Microsoft Azure Stream Analytics Tools for Visual Studio C#-Tools 4.2.0-4.22281.5+8d3180e5f00d42f0f0295165f756f368f0cbfa44 C#-Komponenten, die in der IDE verwendet werden. Abhängig von Ihrem Projekttyp und den zugehörigen Einstellungen kann eine andere Version des Compilers verwendet werden. Common Azure Tools 1.10 Provides common services for use by Azure Mobile Services and Microsoft Azure Tools. Cookiecutter 17.0.22089.1 Stellt Tools zum Suchen, Instanziieren und Anpassen von Vorlagen im Cookiecutter-Format bereit. Extensibility Message Bus 1.2.6 (master@34d6af2) Provides common messaging-based MEF services for loosely coupled Visual Studio extension components communication and integration. Linux-Kernspeicherabbild debuggen 1.0.9.32408 Aktiviert das Debuggen von Linux-Kernspeicherabbildern. Microsoft Azure-Tools für Visual Studio 2.9 Support für Azure Cloud Services-Projekte Microsoft Azure Hive Query Language Service 2.6.5000.0 Language service for Hive query Microsoft Azure Stream Analytics Language Service 2.6.5000.0 Language service for Azure Stream Analytics Microsoft JVM Debugger 1.0 Provides support for connecting the Visual Studio debugger to JDWP compatible Java Virtual Machines Mono Debugging for Visual Studio 17.2.20 (482eb2a) Support for debugging Mono processes with Visual Studio. Node.js-Tools 1.5.40404.3 Commit Hash:dd524a7d0ee653de8b7b32a93ca22f2d57cd2419 Fügt Unterstützung für das Entwickeln und Debuggen von Node.js-Apps in Visual Studio hinzu NuGet-Paket-Manager 6.2.1 NuGet-Paket-Manager in Visual Studio. Weitere Informationen zu NuGet finden Sie unter https://docs.nuget.org/. Office Developer Tools für Visual Studio 17.0.32314.00 Microsoft Office Developer Tools für Visual Studio Python – Django-Unterstützung 17.0.22089.1 Stellt Vorlagen und Integration für das Django-Webframework bereit. Python – Unterstützung für die Profilerstellung 17.0.22089.1 Profilerstellungsunterstützung für Python-Projekte. Python mit Pylance 17.0.22089.1 Stellt IntelliSense, Projekte, Vorlagen, Debuggen, interaktive Fenster und weitere Unterstützung für Python-Entwickler bereit. Razor (ASP.NET Core) 17.0.0.2218101+885a343b00bcab620a90c1550c37dafd730ce984 Stellt Sprachdienste für ASP.NET Core Razor bereit. SQL Server Data Tools 17.0.62204.01010 Microsoft SQL Server Data Tools Syntax Visualizer 1.0 An extension for visualizing Roslyn SyntaxTrees. Testadapter für Boost.Test 1.0 Aktiviert Visual Studio-Testtools mit Komponententests, die für Boost.Test geschrieben wurden. Nutzungsbedingungen und Drittanbieterhinweise stehen im Installationsverzeichnis der Erweiterung zur Verfügung. Testadapter für Google Test 1.0 Ermöglicht die Verwendung von Visual Studio-Testtools mit Komponententests, die für Google Test geschrieben wurden. Die Nutzungsbedingungen und Drittanbieterhinweise stehen im Installationsverzeichnis der Erweiterung zur Verfügung. ToolWindowHostedEditor 1.0 Hosting json editor into a tool window TypeScript-Tools 17.0.10418.2001 TypeScript-Tools für Microsoft Visual Studio Viasfora 4.4.212 Add color to your Visual Studio editor! Visual Basic-Tools 4.2.0-4.22281.5+8d3180e5f00d42f0f0295165f756f368f0cbfa44 Visual Basic-Komponenten, die in der IDE verwendet werden. Abhängig von Ihrem Projekttyp und den zugehörigen Einstellungen kann eine andere Version des Compilers verwendet werden. Visual C++ für Linux-Entwicklung 1.0.9.32408 Visual C++ für Linux-Entwicklung Visual C++ für plattformübergreifende Mobile-Entwicklung (Android) 17.0.32408.312 Visual C++ für plattformübergreifende Mobile-Entwicklung (Android) Visual F# Tools 17.1.0-beta.22329.1+702b8e77f5fbfe21e6743324c1750503e02f182d Microsoft Visual F# Tools Visual Studio-Tools für Unity 17.2.3.0 Visual Studio-Tools für Unity Visual Studio Color Theme Designer 1.0 Allows users to create and edit Visual Studio themes. Themes can customize colors used for menus, toolbars, tabs, titlebars, the text editor, and other environment colors. Visual Studio IntelliCode 2.2 KI-gestützte Entwicklung für Visual Studio. VisualStudio.DeviceLog 1.0 Informationen zum Paket VisualStudio.Mac 1.0 Mac Extension for Visual Studio VSPackage Extension 1.0 VSPackage Visual Studio Extension Detailed Info Workflow Manager Tools 1.0 1.0 Dieses Paket enthält die erforderlichen Visual Studio-Integrationskomponenten für Workflow Manager. Xamarin 17.2.0.177 (d17-2@2f6c881) Visual Studio-Erweiterung, um Entwicklung für Xamarin.iOS und Xamarin.Android zu ermöglichen. Xamarin Designer 17.2.0.244 (remotes/origin/d17-2@197e1a0b7) Visual Studio-Erweiterung zum Aktivieren der Xamarin Designer-Tools in Visual Studio. Xamarin Templates 17.2.15 (2e3b60e) Templates for building iOS, Android, and Windows apps with Xamarin and Xamarin.Forms. Xamarin.Android SDK 12.3.3.3 (d17-2/4e061b7) Xamarin.Android Reference Assemblies and MSBuild support. Mono: dffa5ab Java.Interop: xamarin/java.interop/d17-2@9760f0a9 ProGuard: Guardsquare/proguard/v7.0.1@912d149 SQLite: xamarin/sqlite/3.38.2@7b1e016 Xamarin.Android Tools: xamarin/xamarin-android-tools/d17-2@fc3c2ac Xamarin.iOS and Xamarin.Mac SDK 15.10.0.5 (96b3edb6d) Xamarin.iOS and Xamarin.Mac Reference Assemblies and MSBuild support. ```

Binary logs are preferred instead of text logs. Please save and attach a .binlog if possible: (https://docs.microsoft.com/en-us/visualstudio/msbuild/obtaining-build-logs-with-msbuild?view=vs-2019#save-a-binary-log)

Screenshots

error

BlyZeYT commented 2 years ago

Yeah don't ask about that Minecraft skin bottom right

michaelchanna commented 2 years ago

I am experiencing this as well. Page would load fine and then all of a sudden anytime I go to it I get a Null Ref Exception.

This also started happening on another page in my app.

This is from Debug Log...

[AppCenter] HTTP response status=200 payload={"status":"Success","validDiagnosticsIds":[],"throttledDiagnosticsIds":[],"correlationId":"81ec9d5d-865d-4281-a24f-2e8c42fa308d"} [AppCenterCrashes] Unhandled Exception from source=AppDomain [AppCenterCrashes] System.NullReferenceException: Object reference not set to an instance of an object. [AppCenterCrashes] at Android.Runtime.JNINativeWrapper._unhandled_exception (System.Exception e) [0x0000e] in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.Runtime/JNINativeWrapper.g.cs:12 [AppCenterCrashes] at Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PPLLL_L (_JniMarshal_PPLLL_L callback, System.IntPtr jnienv, System.IntPtr klazz, System.IntPtr p0, System.IntPtr p1, System.IntPtr p2) [0x00022] in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.Runtime/JNINativeWrapper.g.cs:298 [AppCenterCrashes] at (wrapper native-to-managed) Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PPLLL_L(intptr,intptr,intptr,intptr,intptr) [AppCenterCrashes] Saving uncaught exception. [AppCenterCrashes] Saved JSON content for ingestion into /data/user/0/com.barwatch.app.v1/files/error/77af0b82-6449-4109-8c41-9063463072f3.json [AppCenterCrashes] Saved raw wrapper exception data into /data/user/0/com.barwatch.app.v1/files/error/77af0b82-6449-4109-8c41-9063463072f3.dat [mono-rt] [ERROR] FATAL UNHANDLED EXCEPTION: System.NullReferenceException: Object reference not set to an instance of an object. [mono-rt] at Android.Runtime.JNINativeWrapper._unhandled_exception (System.Exception e) [0x0000e] in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.Runtime/JNINativeWrapper.g.cs:12 [mono-rt] at Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PPLLL_L (_JniMarshal_PPLLL_L callback, System.IntPtr jnienv, System.IntPtr klazz, System.IntPtr p0, System.IntPtr p1, System.IntPtr p2) [0x00022] in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.Runtime/JNINativeWrapper.g.cs:298 [mono-rt] at (wrapper native-to-managed) Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PPLLL_L(intptr,intptr,intptr,intptr,intptr)

konradzuse commented 2 years ago

I experience this also, it appears to be related to pushing pages onto the navigation stack too quickly on startup;

System.NullReferenceException: Object reference not set to an instance of an object. at Android.Runtime.JNINativeWrapper._unhandled_exception (System.Exception e) [0x0000e] in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.Runtime/JNINativeWrapper.g.cs:12 at Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PP_V (_JniMarshal_PP_V callback, System.IntPtr jnienv, System.IntPtr klazz) [0x0001c] in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.Runtime/JNINativeWrapper.g.cs:23 at (wrapper native-to-managed) Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PP_V(intptr,intptr)

BlyZeYT commented 2 years ago

I experience this also, it appears to be related to pushing pages onto the navigation stack too quickly on startup;

System.NullReferenceException: Object reference not set to an instance of an object. at Android.Runtime.JNINativeWrapper._unhandled_exception (System.Exception e) [0x0000e] in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.Runtime/JNINativeWrapper.g.cs:12 at Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PP_V (_JniMarshal_PP_V callback, System.IntPtr jnienv, System.IntPtr klazz) [0x0001c] in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.Runtime/JNINativeWrapper.g.cs:23 at (wrapper native-to-managed) Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PP_V(intptr,intptr)

This even Happens when I replace the Navigation Page in Flyout.Detail with a Content page

jfversluis commented 2 years ago

What versions of Xamarin.Forms are you using? Did it happen with a previous version as well?

BlyZeYT commented 2 years ago

What versions of Xamarin.Forms are you using? Did it happen with a previous version as well?

I'm using Xamarin.Forms 5.0.0.2478. It worked on this version and all of a sudden it doesn't anymore

I can try downgrading.

BlyZeYT commented 2 years ago

On Xamarin.Forms 5.0.0.2401 the same happens with the same exception

Imodejam commented 2 years ago

I have the same problem. I use the Xamarin.Forms 5.0.0.2401

neljaj commented 2 years ago

I have the same issue, but I am not even using Forms. I use Xamarin.Android.

BlyZeYT commented 2 years ago

I fixed the issue.

I put a try catch on that InitializeComponent(); and there I got the error by hovering over the catched exception in debug.

public Generator()
        {
            try
            {
                InitializeComponent();
            }
            catch (Exception ex)
            {
                throw ex;
            }

            flyout.qrcodeMenuList.ItemSelected += OnSelectedItem;
        }

And guess what the error was...

The FlyoutPage was the problem not the Generator Page where the exception was thrown.

I didn't had a title set on the FlyoutPage and because of that it crashed.

So if you have an error and get a meaningless exception, try to set the line where the exception happens in a try catch block and maybe you get more information.

I hope it helps someone who also has such a or similar error.

Keep going guys :)