NULL error with FindByName

Jassim Al Rahma 1,526 Reputation points
2021-10-29T23:30:16.56+00:00

Hi,

I am getting:

Object reference not set to an instance of an object

when trying FindByName this way:

StackLayoutPostDetails.FindByName<Label>("LabelPostGender").Text = "OK";

Here is my XAML:

<StackLayout x:Name="StackLayoutPostDetails" HorizontalOptions="FillAndExpand"  VerticalOptions="FillAndExpand">
    <BindableLayout.ItemTemplate>
        <DataTemplate>
            <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>

            <Grid Grid.Row="1" Margin="0,10,5,5">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>

                <Image Grid.Column="0" WidthRequest="30" HeightRequest="30" Margin="5" HorizontalOptions="Center" VerticalOptions="End">
                <Image.Source>
                    <FontImageSource FontFamily="FontRegular" Glyph="&#xf044;" Color="Red" />
                </Image.Source>
                <Image.GestureRecognizers>
                    <TapGestureRecognizer Tapped="TapGestureRecognizerShowGender_Tapped" NumberOfTapsRequired="1" />
                </Image.GestureRecognizers>
                </Image>

                <StackLayout Grid.Column="1" Orientation="Horizontal" Margin="5" >
                    <Label Text="Gender : " VerticalOptions="Center" />
                    <Label x:Name="LabelPostGender" Text="{Binding gender_name}" VerticalOptions="Center" />
                </StackLayout>
            </Grid>
            </Grid>
            </DataTemplate>
    </BindableLayout.ItemTemplate>
</StackLayout>

Thanks,
Jassim

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,296 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,301 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 68,741 Reputation points Microsoft Vendor
    2021-11-01T05:16:29.863+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    Get your control by FindByName<> in your DataTemplate code. It is not recommand way. You could get the control and set the value by Data Binding.

    https://learn.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/data-binding/

    Because they are in different NameScopes if you use this datatemplate in the listview or collectionview. The Xaml parser creates one NameScope per Xaml root object in your file, and then different NameScopes for DataTemplates. Here is a similar thread:https://stackoverflow.com/a/42854892

    Do you want to achieve the function that click the Image, then text of LabelPostGender will be changed?

    If so, I achieve simple code with data-binding.

    First of all, I create a model called PlatformInfo.cs, it achieve the INotifyPropertyChanged for update at the runtime.

       public class PlatformInfo : INotifyPropertyChanged  
           {  
               private bool _isChecked;  
               private string _platformName;  
         
               public bool IsChecked  
               {  
                   get { return _isChecked; }  
                   set { _isChecked = value; NotifyPropertyChanged(); }  
               }  
         
               public string PlatformName  
               {  
                   get { return _platformName; }  
                   set { _platformName = value; NotifyPropertyChanged(); }  
               }  
         
               public event PropertyChangedEventHandler PropertyChanged;  
               public void NotifyPropertyChanged([System.Runtime.CompilerServices.CallerMemberName] string propertyName = "")  
               {  
                   if (this.PropertyChanged != null)  
                       this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));  
               }  
           }  
    

    Then I achieve a viewModel, that I add binding data and command. ChangeTextCommmand will binding to the Image's TapGestureRecognizer command. If this command is invoked, PlatformName's text will be changed.

       public class ViewModel  
           {  
               public ViewModel()  
               {  
                   this.GetContactsList();  
               }  
         
               public List<PlatformInfo> PlatformsList { get; set; }  
               public ICommand ChangeTextCommmand { get; set; }  
         
               private void GetContactsList()  
               {  
                   if (this.PlatformsList == null)  
                       this.PlatformsList = new List<PlatformInfo>();  
         
                   this.PlatformsList.Add(new PlatformInfo() { IsChecked = true, PlatformName = "Android" });  
                   this.PlatformsList.Add(new PlatformInfo() { IsChecked = true, PlatformName = "iOS" });  
                   this.PlatformsList.Add(new PlatformInfo() { IsChecked = false, PlatformName = "UWP" });  
         
                   ChangeTextCommmand=new Command((Myobject) =>  
                   {  
                       PlatformInfo  platformInfo= Myobject as PlatformInfo;  
         
                       platformInfo.PlatformName = "tes";  
                   });  
               }  
           }  
    

    Here is test xaml layout.

       <?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:imagelongclickdemo="clr-namespace:ImageLongClickDemo"  
                    x:Class="ImageLongClickDemo.MainPage">  
         
           <ContentPage.BindingContext>  
               <imagelongclickdemo:ViewModel />  
           </ContentPage.BindingContext>  
           <StackLayout x:Name="StackLayoutPostDetails" HorizontalOptions="FillAndExpand"  VerticalOptions="FillAndExpand" BindableLayout.ItemsSource="{Binding PlatformsList}"     >  
         
               
               <BindableLayout.ItemTemplate>  
                       <DataTemplate>  
                           <Grid>  
                               <Grid.RowDefinitions>  
                                   <RowDefinition Height="Auto" />  
                                   <RowDefinition Height="Auto" />  
                               </Grid.RowDefinitions>  
         
                               <Grid Grid.Row="1" Margin="0,10,5,5">  
                                   <Grid.ColumnDefinitions>  
                                       <ColumnDefinition Width="Auto" />  
                                       <ColumnDefinition Width="*" />  
                                   </Grid.ColumnDefinitions>  
                                 
                               <Image x:Name="myImage" Grid.Column="0" WidthRequest="30" Source="icon1.png" HeightRequest="30" Margin="5" HorizontalOptions="Center" VerticalOptions="End">  
                                        
                                       <Image.GestureRecognizers>  
                                       <TapGestureRecognizer Command="{Binding BindingContext.ChangeTextCommmand , Source={x:Reference Name=StackLayoutPostDetails}}" CommandParameter="{Binding .}" NumberOfTapsRequired="1" />  
                                       </Image.GestureRecognizers>  
                               </Image>  
         
                                   <StackLayout x:Name="Mysl" Grid.Column="1" Orientation="Horizontal" Margin="5" >  
                                       <Label Text="Gender : " VerticalOptions="Center" />  
                                       <Label x:Name="LabelPostGender" Text="{Binding PlatformName}" VerticalOptions="Center" />  
                                   </StackLayout>  
                               </Grid>  
                           </Grid>  
                       </DataTemplate>  
                   </BindableLayout.ItemTemplate>  
               </StackLayout>  
            
            
         
       </ContentPage>  
    

    Best Regards,

    Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.