System.NullReferenceException: Object reference not set to an instance of an object.

Samuel Pimenta 45 Reputation points
2024-01-13T13:03:49.4533333+00:00

Hello, I am developing this app for iOS iPad on macos m1 Sonoma. Compiles correctly everything ok. I use visual studio 2022 for mac and and .net7. The problem is when I click on the 'Login' button. At the moment I only need that when I click on the button, I navigate to DashboardPage. This error appears:

It also happens if I use an Android emulator. Program.cs

using ObjCRuntime;
using UIKit;

namespace WisdomPetMedicine;

public class Program
{
    // This is the main entry point of the application.
    static void Main(string[] args)
    {
        // if you want to use a different Application Delegate class from "AppDelegate"
        // you can specify it here.
        UIApplication.Main(args, null, typeof(AppDelegate));
    }
}

LoginPage.xaml

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="AtrianBakers.Pages.LoginPage"
             xmlns:Componentes="clr-namespace:AtrianBakers.Components.Buttons"
             Title="Inicio"
             NavigationPage.HasNavigationBar="False"
             BackgroundColor="{DynamicResource Background}">
    
    <Grid
        RowDefinitions="*,auto,*"
        HorizontalOptions="FillAndExpand"
        VerticalOptions="Center"
        Margin="16">

        <Frame
            Padding="0"
            CornerRadius="8"
            IsClippedToBounds="True">

            <Grid
                ColumnDefinitions="*,*"
                BackgroundColor="White">

                <Grid
                    RowDefinitions="Auto,Auto,Auto,Auto"
                    Padding="16"
                    VerticalOptions="Center">

                    

                    <Label
                        Grid.Row="1"
                        Text="Iniciar sesión"
                        FontSize="Title"
                        TextColor="Black"/>

                    <Label
                        Grid.Row="2"
                        Text="Bienvenid@ a la plataforma comercial"
                        FontSize="Body"
                        TextColor="Black"
                        Margin="0,16,0,0"/>

                <!-- <Componentes:ButtonPrimary 
                        Grid.Row="3"
                        Margin="0,24,0,0"
                        x:Name="ButtonPrimary"
                        Clicked="ButtonPrimary_Clicked"
                        Text="Texto"/> -->
                    <Button
                        Grid.Row="3"
                        Text="login"
                        Margin="0,24,0,0"
                        HorizontalOptions="Start"
                        Clicked="OnLoginButtonClicked">
                    </Button>

                </Grid>

                <Image
                    Grid.Row="0"
                    Grid.Column="1"
                    Source="img_login.jpg"
                    Aspect="AspectFill"/>         
            </Grid>

        </Frame>
        
    </Grid>

           
    
</ContentPage>

LoginPage.xaml.cs

namespace AtrianBakers.Pages;

public partial class LoginPage : ContentPage
{
    public LoginPage()
    {
        InitializeComponent();
        // Aquí puedes agregar cualquier inicialización adicional si es necesario.
    }

    private async void BtnOnLoginButton_Clicked(object sender, EventArgs e)
    {
        try
        {
            // Navegar a DashboardPage usando Shell
            await Shell.Current.GoToAsync("NewPage1");
        }
        catch (Exception ex)
        {
            // Manejar la excepción si ocurre algún error durante la navegación
            Console.WriteLine(ex.Message);
        }
    }


}

IMPORTANT: When I click on the login button, it paints this message on the console: "Object reference not set to an instance of an object."

AppShell.xaml

<?xml version="1.0" encoding="UTF-8" ?>
<Shell
    x:Class="AtrianBakers.AppShell"
    xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:local="clr-namespace:AtrianBakers.Pages"
    Shell.FlyoutBehavior="Disabled">

    <!-- LoginPage -->
    <ShellContent
        ContentTemplate="{DataTemplate local:LoginPage}"
        Route="LoginPage"/>

    <!-- DashboardPage -->
    <ShellContent
        ContentTemplate="{DataTemplate local:DashboardPage}"
        Route="DashboardPage"/>
        
</Shell>

AppShell.xaml.cs

namespace AtrianBakers;


public partial class AppShell : Shell
{
	public AppShell()
	{
		InitializeComponent();
	}
}

I don't know if this has to do with the error: When I try to update, it shows compatibility error.Captura de pantalla 2024-01-13 a las 11.48.52

Frontend on emulator ipad User's image

Can you help me? Thanks

Developer technologies .NET .NET MAUI
{count} votes

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.