Strange NullReferenceException and useless Call Stack

YuraBabiyYuraBabiy UAMember ✭✭✭
edited September 2016 in Xamarin.Forms

I decided to change layout in my xaml page from absolut to stacklayout and after that I had nullre.. exception when my app starts running. I commented from one to another all elements in page, even stacklayout, but I still have error. I tried to use Call Stack at first time, but I don't see what I can do with it usefull.
Before I started to change my page, all was working well.
Besides, it is Master page for MasterDetailPage.
Xaml.cs file is empty.
Here is xaml now:
`<?xml version="1.0" encoding="utf-8"?>

        <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
                 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                 xmlns:prism="clr-namespace:Prism.Mvvm;assembly=Prism.Forms"
                 xmlns:b="clr-namespace:Xamarin.Behaviors;assembly=Xamarin.Behaviors"
                 prism:ViewModelLocator.AutowireViewModel="True"
                 x:Class="GitRemote.Views.MasterPage"
                 Title="Necessary">

      <!--<StackLayout x:Name="MasterMenu"
                   HorizontalOptions="StartAndExpand"
                   VerticalOptions="StartAndExpand">

        --><!--<BoxView x:Name="GrayHeader"
                 Color="#CACED2"
                 HorizontalOptions="StartAndExpand"
                 VerticalOptions="Start"
                 HeightRequest="100"/>--><!--


        --><!--<Image x:Name="MasterProfileImage"
               WidthRequest="50"
               HeightRequest="50"
               Source="ic_account_circle_white_24dp.png"
               HorizontalOptions="Start"
               VerticalOptions="Start"
               Margin="15, 15, 0, 0"/>--><!--

        --><!--<Image x:Name="LogOutImage"
               WidthRequest="45"
               HeightRequest="45"
               Source="ic_menu_logout.png"
               HorizontalOptions="End"
               VerticalOptions="Start"
               Margin="0, 15, 15, 0"/>--><!--

        --><!--<Label x:Name="MasterProfileName"
               Text="GitRemote"
               FontAttributes="Bold"
               FontSize="18"
               HorizontalOptions="Start"
               VerticalOptions="Start"
               TextColor="Black"
               Margin="15, 80, 0, 0"/>--><!--


        --><!--<ListView x:Name="MasterPageMenu"
                  SeparatorColor="Transparent"
                  HorizontalOptions="Start"
                  VerticalOptions="Start"
                  Margin="15, 120, 0, 0"
                  ItemsSource="{Binding MenuItems}"
                  SelectedItem="{Binding MenuItemSelectedProperty, Mode=TwoWay}">
          <ListView.ItemTemplate>
            <DataTemplate>
              <ViewCell>
                <StackLayout Orientation="Horizontal"
                             Spacing="15"
                             Padding="10" >
                  <Label Text="{Binding Name}"
                         FontSize="16"
                         VerticalOptions="Center"
                         FontAttributes="Bold"/>
                  <Image Source="{Binding ImageSource}"
                         WidthRequest="25"
                         HeightRequest="25" />
                </StackLayout>
              </ViewCell>
            </DataTemplate>
          </ListView.ItemTemplate>
          <b:Interaction.Behaviors>
            <b:BehaviorCollection>
              <b:EventToCommand EventName="ItemSelected"
                                Command="{Binding MenuItemSelected}"></b:EventToCommand>
            </b:BehaviorCollection>
          </b:Interaction.Behaviors>
        </ListView>--><!--

      </StackLayout>-->

    </ContentPage>`

**This is how it looked before:
**
`<?xml version="1.0" encoding="utf-8"?>

<AbsoluteLayout x:Name="MasterMenu">

    <!--Header-->
    <BoxView x:Name="GrayHeader"
             Style="{StaticResource GrayBoxView}"
             AbsoluteLayout.LayoutBounds="{Binding MasterProfileGrayHeaderBounds}"
             AbsoluteLayout.LayoutFlags="All">
    </BoxView>

    <Image x:Name="MasterProfileImage"
           WidthRequest="50"
           HeightRequest="50"
           Source="ic_account_circle_white_24dp.png"
           AbsoluteLayout.LayoutBounds="{Binding MasterProfileImageBounds}"
           AbsoluteLayout.LayoutFlags="None">
    </Image>

    <Label x:Name="MasterProfileName"
           Text="GitRemote"
           FontAttributes="Bold"
           FontSize="18"
           AbsoluteLayout.LayoutBounds="{Binding MasterProfileNameBounds}"
           AbsoluteLayout.LayoutFlags="None">
    </Label>

    <!--Body-->

    <ListView x:Name="MasterPageMenu"
              SeparatorColor="Transparent"
              AbsoluteLayout.LayoutBounds="{Binding MasterMenuBounds}"
              AbsoluteLayout.LayoutFlags="None"
              ItemsSource="{Binding MenuItems}"
              SelectedItem="{Binding MenuItemSelectedProperty, Mode=TwoWay}">
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                    <StackLayout Orientation="Horizontal" Spacing="15" Padding="10" > 
                        <Label Text="{Binding Name}" FontSize="16" VerticalOptions="Center" FontAttributes="Bold"/>
                        <Image Source="{Binding ImageSource}" WidthRequest="25" HeightRequest="25" />
                    </StackLayout>
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
        <b:Interaction.Behaviors>
          <b:BehaviorCollection>
            <b:EventToCommand EventName="ItemSelected" Command="{Binding MenuItemSelected}"></b:EventToCommand>
          </b:BehaviorCollection>
        </b:Interaction.Behaviors>
    </ListView> 

</AbsoluteLayout>

`

Here is my ViewModel now:

`using GitRemote.Models;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Navigation;
using System.Collections.ObjectModel;

namespace GitRemote.ViewModels
{
public class MasterPageViewModel : BindableBase
{

    #region Constants
    private const string GistsPageImagePath = "ic_code_black_24dp.png";
    private const string IssueDashboardPageImagePath = "ic_slow_motion_video_black_24dp.png";
    private const string BookmarksPageImagePath = "ic_bookmark_black_24dp.png";
    private const string ReportAnIssuePageImagePath = "ic_error_outline_black_24dp.png";
    #endregion

    //#region Bindable properties
    //private Rectangle _masterProfileGrayHeaderBounds;

    //public Rectangle MasterProfileGrayHeaderBounds
    //{
    //    get { return _masterProfileGrayHeaderBounds; }
    //    set { SetProperty(ref _masterProfileGrayHeaderBounds, value); }
    //}

    //private Rectangle _masterProfileImageBounds;

    //public Rectangle MasterProfileImageBounds
    //{
    //    get { return _masterProfileImageBounds; }
    //    set { SetProperty(ref _masterProfileImageBounds, value); }
    //}

    //private Rectangle _masterProfileNameBounds;

    //public Rectangle MasterProfileNameBounds
    //{
    //    get { return _masterProfileNameBounds; }
    //    set { SetProperty(ref _masterProfileNameBounds, value); }
    //}

    //private Rectangle _masterMenuBounds;

    //public Rectangle MasterMenuBounds
    //{
    //    get { return _masterMenuBounds; }
    //    set { SetProperty(ref _masterMenuBounds, value); }
    //}

    //private MasterPageMenuItemModel _menuItemSelectedProperty;

    //public MasterPageMenuItemModel MenuItemSelectedProperty
    //{
    //    get { return _menuItemSelectedProperty; }
    //    set { SetProperty(ref _menuItemSelectedProperty, value); }
    //}
    ////#endregion

    //private readonly INavigationService _navigationService;

    //public ObservableCollection<MasterPageMenuItemModel> MenuItems;

    //public DelegateCommand MenuItemSelected;

    public MasterPageViewModel()
    {
        //_navigationService = navigationService;
        //MenuItemSelected = new DelegateCommand(OnMenuItemSelected);

        //MasterProfileGrayHeaderBounds = new Rectangle(0, 0, 1, 0.175);
        //MasterProfileImageBounds = new Rectangle(16, 16, AbsoluteLayout.AutoSize, AbsoluteLayout.AutoSize);
        //MasterProfileNameBounds = new Rectangle(16, 70, AbsoluteLayout.AutoSize, AbsoluteLayout.AutoSize);
        //MasterMenuBounds = new Rectangle(16, 100 + 10, AbsoluteLayout.AutoSize, AbsoluteLayout.AutoSize);

        //MenuItems = new ObservableCollection<MasterPageMenuItemModel>
        //{
        //    new MasterPageMenuItemModel {Name = "GistsPage", ImageSource = GistsPageImagePath},
        //    new MasterPageMenuItemModel {Name = "IssueDashboardPage", ImageSource = IssueDashboardPageImagePath},
        //    new MasterPageMenuItemModel {Name = "BookmarksPage", ImageSource = BookmarksPageImagePath},
        //    new MasterPageMenuItemModel {Name = "ReportAnIssuePage", ImageSource = ReportAnIssuePageImagePath}
        //};
    }

    //private void OnMenuItemSelected()
    //{
    //    if ( MenuItemSelectedProperty == null ) return;
    //    _navigationService.NavigateAsync(MenuItemSelectedProperty.Name, animated: false);
    //    MenuItemSelectedProperty = null;
    //}
}

}
`

And before:

using GitRemote.Models;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Navigation;
using System.Collections.ObjectModel;
using Xamarin.Forms;

namespace GitRemote.ViewModels
{
public class MasterPageViewModel : BindableBase
{

    #region Constants
    private const string GistsPageImagePath = "ic_code_black_24dp.png";
    private const string IssueDashboardPageImagePath = "ic_slow_motion_video_black_24dp.png";
    private const string BookmarksPageImagePath = "ic_bookmark_black_24dp.png";
    private const string ReportAnIssuePageImagePath = "ic_error_outline_black_24dp.png";
    #endregion

    #region Bindable properties
    private Rectangle _masterProfileGrayHeaderBounds;

    public Rectangle MasterProfileGrayHeaderBounds
    {
        get { return _masterProfileGrayHeaderBounds; }
        set { SetProperty(ref _masterProfileGrayHeaderBounds, value); }
    }

    private Rectangle _masterProfileImageBounds;

    public Rectangle MasterProfileImageBounds
    {
        get { return _masterProfileImageBounds; }
        set { SetProperty(ref _masterProfileImageBounds, value); }
    }

    private Rectangle _masterProfileNameBounds;

    public Rectangle MasterProfileNameBounds
    {
        get { return _masterProfileNameBounds; }
        set { SetProperty(ref _masterProfileNameBounds, value); }
    }

    private Rectangle _masterMenuBounds;

    public Rectangle MasterMenuBounds
    {
        get { return _masterMenuBounds; }
        set { SetProperty(ref _masterMenuBounds, value); }
    }

    private MasterPageMenuItemModel _menuItemSelectedProperty;

    public MasterPageMenuItemModel MenuItemSelectedProperty
    {
        get { return _menuItemSelectedProperty; }
        set { SetProperty(ref _menuItemSelectedProperty, value); }
    }
    #endregion

    private readonly INavigationService _navigationService;

    public ObservableCollection<MasterPageMenuItemModel> MenuItems;

    public DelegateCommand MenuItemSelected;

    public MasterPageViewModel(INavigationService navigationService)
    {
        _navigationService = navigationService;
        MenuItemSelected = new DelegateCommand(OnMenuItemSelected);

        MasterProfileGrayHeaderBounds = new Rectangle(0, 0, 1, 0.175);
        MasterProfileImageBounds = new Rectangle(16, 16, AbsoluteLayout.AutoSize, AbsoluteLayout.AutoSize);
        MasterProfileNameBounds = new Rectangle(16, 70, AbsoluteLayout.AutoSize, AbsoluteLayout.AutoSize);
        MasterMenuBounds = new Rectangle(16, 100 + 10, AbsoluteLayout.AutoSize, AbsoluteLayout.AutoSize);

        MenuItems = new ObservableCollection<MasterPageMenuItemModel>
        {
            new MasterPageMenuItemModel {Name = "GistsPage", ImageSource = GistsPageImagePath},
            new MasterPageMenuItemModel {Name = "IssueDashboardPage", ImageSource = IssueDashboardPageImagePath},
            new MasterPageMenuItemModel {Name = "BookmarksPage", ImageSource = BookmarksPageImagePath},
            new MasterPageMenuItemModel {Name = "ReportAnIssuePage", ImageSource = ReportAnIssuePageImagePath}
        };
    }

    private void OnMenuItemSelected()
    {
        if ( MenuItemSelectedProperty == null ) return;
        _navigationService.NavigateAsync(MenuItemSelectedProperty.Name, animated: false);
        MenuItemSelectedProperty = null;
    }
}

}

I commented all stuff and I have no idea where and why it occuers.

On image is my call stack. But I don't know what it means and I can't do anything with it.

Best Answer

Answers

  • RobertMcIntoshRobertMcIntosh CAUniversity ✭✭

    Is your apps MainPage being set somewhere?

  • YuraBabiyYuraBabiy UAMember ✭✭✭
    edited September 2016

    @RobertMcIntosh
    How close is Xamarin community :)
    Here is My App.xaml.cs:
    public partial class App
    {
    protected override void OnInitialized()
    {
    InitializeComponent();
    //NavigationService.NavigateAsync($"{nameof(StartPage)}");
    //NavigationService.NavigateAsync($"{nameof(NavigationBarPage)}/{nameof(TwoFactorAuthPage)}");
    NavigationService.NavigateAsync($" {
    nameof(ProfilePage)}/{nameof(NavigationBarPage)}/{nameof(DetailPage)}");
    }
    protected override void RegisterTypes()
    {
    Container.RegisterTypeForNavigation();
    Container.RegisterTypeForNavigation();
    Container.RegisterTypeForNavigation();
    Container.RegisterTypeForNavigation();
    Container.RegisterTypeForNavigation();
    Container.RegisterTypeForNavigation();
    Container.RegisterTypeForNavigation();
    Container.RegisterTypeForNavigation();
    Container.RegisterTypeForNavigation();
    Container.RegisterTypeForNavigation();
    Container.RegisterTypeForNavigation();
    Container.RegisterTypeForNavigation();
    Container.RegisterTypeForNavigation();
    Container.RegisterTypeForNavigation();
    Container.RegisterTypeForNavigation();
    Container.RegisterTypeForNavigation();
    Container.RegisterTypeForNavigation();
    }

                        protected override void OnSleep()
                        {
                            base.OnSleep();
                            UserManager.SaveLastUser();
                        }
                    }
    

    I also delete style from App.xaml so maybe something wrong with that, but I have no mind where and why
    And App.xaml:

    <prism:PrismApplication.Resources>

          <Color x:Key="LogInWithExistUserBackgroundColor">#054678</Color>
          <Color x:Key="CreateNewUserButtonBackgroundColor">#245625</Color>
    
        </ResourceDictionary>
      </prism:PrismApplication.Resources>
    </prism:PrismApplication>
    

    There is something wrong with code dispaying so I attached file

  • RobertMcIntoshRobertMcIntosh CAUniversity ✭✭

    I don't really know anything about NavigationService, but I'm confident that your MainPage is not being set. Certainly it's not explicitly set in your App constructor.

        public partial class App : Application
        {
            public App()
            {
                InitializeComponent();
                MainPage = new NavigationPage(new StandardTipPage());
    
            }
        }
    
  • YuraBabiyYuraBabiy UAMember ✭✭✭

    @RobertMcIntosh
    It don't needs to be set. I am using Prism framework. Read about that. It is usefull thing.

  • JulienRosenJulienRosen CAMember ✭✭✭✭

    this is one of the worst questions i have ever seen. 95% code spam.

  • RobertMcIntoshRobertMcIntosh CAUniversity ✭✭

    @YuraBabiy somewhere that MasterPage has to be set. Could be done by framework or explicitly, but right now, I'm pretty sure that's what's wrong. It's not because of the layout change.

  • YuraBabiyYuraBabiy UAMember ✭✭✭

    @JulienRosen
    I though about that. But there is two sides. One side is yours. But on another side, I though that I don't know where is error can occuer, and for clearer understanding of question and better answers I copied this commented/trash code.

  • YuraBabiyYuraBabiy UAMember ✭✭✭

    @RobertMcIntosh
    I don't think so. It was working all this time without changes in App initialization.

  • RobertMcIntoshRobertMcIntosh CAUniversity ✭✭

    Here is where the stack trace ends up:
    https://github.com/xamarin/Xamarin.Forms/blob/master/Xamarin.Forms.Platform.Android/AppCompat/Platform.cs

    Line 279 is a call to page.Layout. Pretty sure that page is null at that point.

    What is this line supposed to do?

    NavigationService.NavigateAsync($" {
    nameof(ProfilePage)}/{nameof(NavigationBarPage)}/{nameof(DetailPage)}");
    }
    
  • YuraBabiyYuraBabiy UAMember ✭✭✭
    edited October 2016

    @RobertMcIntosh
    It is just path, where I am navigating. Because it is in App class, the program starts from the page, that is in this path.
    Profile page - MasterDetailPage
    NavigationBarPage - just NavigationPage, that adds NavigationBar to the DetailPage
    DetailPage - detail page of MasterDetailPage.
    In Prism u can declare path's like this and it is Navigating from one to another page in this path and adds these pages in navigation stack.

    I don't remember if I did this yesterday, but today I tried to use another path's, that are commented, and they are working. But they leads me to the start of app. Insteand of what I wanted, to go directly to page, that I tested.
    But this path works in another parts of program, when I am going from another pages.
    And in this code file, where is error, I see this code - "if (page is MasterDetailPage)" , so maybe it is something wrong with using MasterDetailPage on start of program. I don't know. But it seems that I should go to the my test page from start, not directly.
    I will try some combinatios with path's. Maybe it will work somehow.

Sign In or Register to comment.