Default MasterDetailPage - System.NullReferenceException

tpmanctpmanc Member ✭✭
edited April 23 in Xamarin.Forms

Hi, I create new project, add MasterDetailPage and getting exception in ios and android previewer:

2019-04-23 23:01:11.9] ERROR: : iOSXamlRenderer had a rendering error: System.NullReferenceException: Object reference not set to an instance of an object
Test.MasterView..ctor () in /Users/tpmanc/Projects/Test/Test/MasterView.xaml.cs:18
Test.App..ctor () in /Users/tpmanc/Projects/Test/Test/App.xaml.cs:18
<unknown method>
System.Reflection.MonoCMethod.InternalInvoke (System.Object obj, System.Object[] parameters, System.Boolean wrapExceptions) in /Library/Frameworks/Xamarin.iOS.framework/Versions/12.4.0.64/src/Xamarin.iOS/mcs/class/corlib/System.Reflection/MonoMethod.cs:667

App.xaml.cs:18:
MainPage = new MyMasterDetailPage();

MasterView.xaml.cs:18:
MasterPage.ListView.ItemSelected += ListView_ItemSelected;

Can anybody please help me? Thanks in advance

Answers

  • jezhjezh Member, Xamarin Team Xamurai

    Could you please share the steps to reproduce this question?

  • tpmanctpmanc Member ✭✭
    edited April 24
    1. Create new project (image 1)

    2. Create MasterDetailPage XAML (image 2)

    3. Change App.xaml.cs (image 3)
      MainPage = new MainPage(); to MainPage = new MyMasterDetailPage();

    4. Build project and now I have error (image 4 and image 5)

    [2019-04-24 10:24:52.9] ERROR: : iOSXamlRenderer had a rendering error: System.NullReferenceException: Object reference not set to an instance of an object
    Test2.MyMasterDetailPage..ctor () in /Users/tpmanc/Projects/Test2/Test2/MyMasterDetailPage.xaml.cs:18
    Test2.App..ctor () in /Users/tpmanc/Projects/Test2/Test2/App.xaml.cs:13
    <unknown method>
    System.Reflection.MonoCMethod.InternalInvoke (System.Object obj, System.Object[] parameters, System.Boolean wrapExceptions) in /Library/Frameworks/Xamarin.iOS.framework/Versions/12.4.0.64/src/Xamarin.iOS/mcs/class/corlib/System.Reflection/MonoMethod.cs:667
    
  • Liêm_NguyễnLiêm_Nguyễn USMember ✭✭✭✭

    Try reference this on YouTube Channel NTL TV

  • Liêm_NguyễnLiêm_Nguyễn USMember ✭✭✭✭

    @tpmanc said:
    1. Create new project (image 1)

    1. Create MasterDetailPage XAML (image 2)

    2. Change App.xaml.cs (image 3)
      MainPage = new MainPage(); to MainPage = new MyMasterDetailPage();

    3. Build project and now I have error (image 4 and image 5)

    [2019-04-24 10:24:52.9] ERROR: : iOSXamlRenderer had a rendering error: System.NullReferenceException: Object reference not set to an instance of an object
    Test2.MyMasterDetailPage..ctor () in /Users/tpmanc/Projects/Test2/Test2/MyMasterDetailPage.xaml.cs:18
    Test2.App..ctor () in /Users/tpmanc/Projects/Test2/Test2/App.xaml.cs:13
    <unknown method>
    System.Reflection.MonoCMethod.InternalInvoke (System.Object obj, System.Object[] parameters, System.Boolean wrapExceptions) in /Library/Frameworks/Xamarin.iOS.framework/Versions/12.4.0.64/src/Xamarin.iOS/mcs/class/corlib/System.Reflection/MonoMethod.cs:667
    

    Show your MasterDetailPage and MasterDetaiPageMaster

  • tpmanctpmanc Member ✭✭

    @Liêm_Nguyễn said:

    @tpmanc said:
    1. Create new project (image 1)

    1. Create MasterDetailPage XAML (image 2)

    2. Change App.xaml.cs (image 3)
      MainPage = new MainPage(); to MainPage = new MyMasterDetailPage();

    3. Build project and now I have error (image 4 and image 5)

    [2019-04-24 10:24:52.9] ERROR: : iOSXamlRenderer had a rendering error: System.NullReferenceException: Object reference not set to an instance of an object
    Test2.MyMasterDetailPage..ctor () in /Users/tpmanc/Projects/Test2/Test2/MyMasterDetailPage.xaml.cs:18
    Test2.App..ctor () in /Users/tpmanc/Projects/Test2/Test2/App.xaml.cs:13
    <unknown method>
    System.Reflection.MonoCMethod.InternalInvoke (System.Object obj, System.Object[] parameters, System.Boolean wrapExceptions) in /Library/Frameworks/Xamarin.iOS.framework/Versions/12.4.0.64/src/Xamarin.iOS/mcs/class/corlib/System.Reflection/MonoMethod.cs:667
    

    Show your MasterDetailPage and MasterDetaiPageMaster

    It works on emulator. This error only in previewer

    MyMasterDetailPage:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    using Xamarin.Forms;
    using Xamarin.Forms.Xaml;
    
    namespace Test2
    {
        [XamlCompilation(XamlCompilationOptions.Compile)]
        public partial class MyMasterDetailPage : MasterDetailPage
        {
            public MyMasterDetailPage()
            {
                InitializeComponent();
                MasterPage.ListView.ItemSelected += ListView_ItemSelected;
            }
    
            private void ListView_ItemSelected(object sender, SelectedItemChangedEventArgs e)
            {
                var item = e.SelectedItem as MyMasterDetailPageMenuItem;
                if (item == null)
                    return;
    
                var page = (Page)Activator.CreateInstance(item.TargetType);
                page.Title = item.Title;
    
                Detail = new NavigationPage(page);
                IsPresented = false;
    
                MasterPage.ListView.SelectedItem = null;
            }
        }
    }
    
    

    MyMasterDetailPageMaster:

    using System;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.ComponentModel;
    using System.Linq;
    using System.Runtime.CompilerServices;
    using System.Text;
    using System.Threading.Tasks;
    
    using Xamarin.Forms;
    using Xamarin.Forms.Xaml;
    
    namespace Test2
    {
        [XamlCompilation(XamlCompilationOptions.Compile)]
        public partial class MyMasterDetailPageMaster : ContentPage
        {
            public ListView ListView;
    
            public MyMasterDetailPageMaster()
            {
                InitializeComponent();
    
                BindingContext = new MyMasterDetailPageMasterViewModel();
                ListView = MenuItemsListView;
            }
    
            class MyMasterDetailPageMasterViewModel : INotifyPropertyChanged
            {
                public ObservableCollection<MyMasterDetailPageMenuItem> MenuItems { get; set; }
    
                public MyMasterDetailPageMasterViewModel()
                {
                    MenuItems = new ObservableCollection<MyMasterDetailPageMenuItem>(new[]
                    {
                        new MyMasterDetailPageMenuItem { Id = 0, Title = "Page 1" },
                        new MyMasterDetailPageMenuItem { Id = 1, Title = "Page 2" },
                        new MyMasterDetailPageMenuItem { Id = 2, Title = "Page 3" },
                        new MyMasterDetailPageMenuItem { Id = 3, Title = "Page 4" },
                        new MyMasterDetailPageMenuItem { Id = 4, Title = "Page 5" },
                    });
                }
    
                #region INotifyPropertyChanged Implementation
                public event PropertyChangedEventHandler PropertyChanged;
                void OnPropertyChanged([CallerMemberName] string propertyName = "")
                {
                    if (PropertyChanged == null)
                        return;
    
                    PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
                }
                #endregion
            }
        }
    }
    
    
Sign In or Register to comment.