Why is the item variable null

AustyAusty Member ✭✭✭
edited July 22 in Xamarin.Forms

The code below is part of the masterdetailpage implementation and the item variable in the code below throws a system.argumentnullexception when a menu in the master page is clicked. After debugging l find that the item variable is null and l dont know why..Find the code below..

        void OnItemSelected(object sender, SelectedItemChangedEventArgs e)
                {
                    var item = e.SelectedItem as MasterPageItem;
                    if (item != null)
                    {
                        Detail = new NavigationPage((Page)Activator.CreateInstance(item.TargetType));
                        masterPage.Menu.SelectedItem = null;
                        IsPresented = false;
                    }
                }

Find below the code for the master page

<?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:local="using:comicAppFinal"
             x:Class="comicAppFinal.MasterPage"
             Icon="hamburger.png"
             Title="Master" >  
  <StackLayout> 
  <StackLayout  Padding="0,100,0,50" BackgroundColor="#282626" 
                Opacity="0.8" >
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                 <RowDefinition Height="Auto" />    
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="40" />
                 <ColumnDefinition Width="250" />
                 <ColumnDefinition Width="40" />
            </Grid.ColumnDefinitions>
            <Button Text="Sigin In" BackgroundColor="Red" 
                    TextColor="White"
                    FontAttributes="Bold"
                    CornerRadius="0"
                    Grid.Row="0"
                    Grid.Column="1"/>
            <Button Text="Create Account"
                    TextColor="White"
                    FontAttributes="Bold"
                    Grid.Row="1"
                    Grid.Column="1" x:Name="createAccount"
                    x:FieldModifier="Public"/>
        </Grid>        
  </StackLayout>
     <ListView x:Name="listView" SeparatorVisibility="None"
               Margin="0,-5,0,-50"
               x:FieldModifier="Public">
        <ListView.ItemsSource>
            <x:Array Type="{x:Type local:MasterPageItem }" >     
                <local:MasterPageItem Title="Home" 
                            IconSource="home_white.png"
                           />
                <local:MasterPageItem Title="Library" 
                            IconSource="library_white.png" />
                <local:MasterPageItem Title="Genres" 
                            IconSource="star_white.png"
                            TargetType="{x:Type local:LoginPage}"/>
                <local:MasterPageItem Title="Account" 
                            IconSource="user_white.png"
                            TargetType="{x:Type local:HomePage}" />
                <local:MasterPageItem Title="Settings" 
                            IconSource="settings_white.png" />
                <local:MasterPageItem Title="My List" 
                            IconSource="list_white.png"/>
                <local:MasterPageItem Title="Privacy Policy" />
                <local:MasterPageItem Title="Publishers" />
                <local:MasterPageItem Title="Terms Of Use"  />    
                </x:Array>     
         </ListView.ItemsSource>         
         <ListView.ItemTemplate>
            <DataTemplate>
             <ViewCell>
             <ViewCell.View>
             <Grid BackgroundColor="#282626" Padding="0,50,0,500" Margin="0" >
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />                 
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="25" />
                <ColumnDefinition Width="50" />                    
                <ColumnDefinition Width="150" /> 
                 <ColumnDefinition Width="Auto" />     
            </Grid.ColumnDefinitions>
                  <Label Text="{Binding Title}" 
                           TextColor="White"
                           FontAttributes="Bold"
                           FontSize="Small"
                           Grid.Row="0"
                           Grid.Column="2"/> 
                    <Image Source="{Binding IconSource}" 
                           Grid.Row="0"
                           Grid.Column="1"/>        

            </Grid>  
            </ViewCell.View>            
            </ViewCell>            
            </DataTemplate>           
         </ListView.ItemTemplate>       

        </ListView>           

</StackLayout> 
</ContentPage>

This above is the code for the master page xaml. Kindly help.. Thanks

Posts

  • NMackayNMackay GBInsider, University mod

    The listview markup needs to used the Tapped event to call that event handler....unless your hooking up to the event in the constructor, you've not posted the constructor so it's hard to know. If you use XAML to handler the event in your code behind, the event handler will be unhooked automatically but if you subscribe yourself you'll have to unhook it before unloading the page...or your get a memory leak.

Sign In or Register to comment.