0

Hi I have a problem I have a collection view and function for checkbox. From checkbox(in collection view) I want to write field in object of my class.But I have the error.I explain.I have 3 pages on the first I have other function on the second I have collection view with check box and label.On the third page I have label which show me information about motortype(class for auto).If on the 2-nd page I choose checkbox and click on button to go on the 3 page I have no problems.I see the value of the field.But if I want to go to the 1 page(for example to see which mark of car I choose) I have the error

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

XAML 2-ND PAGE

 <CollectionView x:Name="listview" ItemsSource="{Binding MotorType}"   Margin="0" HeightRequest="220">
                    <CollectionView.ItemTemplate>
                        <DataTemplate>

                            <StackLayout WidthRequest="300" Orientation="Horizontal" >
                                    <CheckBox HorizontalOptions="Start" Color="Black" CheckedChanged="CheckBox_CheckedChanged"
                                    IsChecked="{Binding IsSelected}"
                                    />
                                    <Label Text="{Binding Name}" TextColor="Gray"></Label>
                                </StackLayout>
                         
                        </DataTemplate>
                    </CollectionView.ItemTemplate>
                </CollectionView>

CODE BEHIND 2-ND PAGE

public partial class Car_add_model : ContentPage
    {
       public MainCar main { get; set;}

        public Car_add_model(MainCar s)
        {
            main = s;           
            NavigationPage.SetHasNavigationBar(this, false);
        
            InitializeComponent();
             BindingContext = new CarAddModel(Navigation, main);
        }
        Motor previousModel;
        private void CheckBox_CheckedChanged(object sender, CheckedChangedEventArgs e)
        {
            if (previousModel != null)
            {
                previousModel.IsSelected = false;
            }
            Motor currentModel = ((CheckBox)sender).BindingContext as Motor;
            previousModel = currentModel;
             main.MotorType = currentModel.Name;        

        }

    }

I HAVE AN ERROR THERE

enter image description here

As I correctly understood the mistake here

currentModel.Name; 

Because I tried to assign a value to a variable in function(the variables were different,I create in function,in ViewModel and in Code Behind )But always I have this error)

class CarAddModel : INotifyPropertyChanged { public ObservableCollection MotorType { get; set; }

        public MainCar mainCar { get; set; }
    public Command navigateCommand { get; set; }

        public Command navigateCommandBACK { get; set; }
        public async Task GotoPage2()
        {
         await Navigation.PushModalAsync(new CarView(mainCar));
        }

        public async Task GotoPage1()
        {
          await Navigation.PopModalAsync();
        }
  public CarAddModel(INavigation navigation, MainCar carC)
        {
            mainCar = carC;
            this.Navigation = navigation;
            this.navigateCommand = new Command(async () => await GotoPage2());
            this.navigateCommandBACK = new Command(async () => await GotoPage1());

        ///////////////////////////////////////
        MotorType = new ObservableCollection<Motor>
            {
               new Motor(){Name="Petrol",IsSelected=false },
                 new Motor(){Name="Diesel" ,IsSelected=false},
                   new Motor(){Name="Gas",IsSelected=false },
                     new Motor(){Name="Petrol / Gas",IsSelected=false },
                       new Motor(){Name="Electric",IsSelected=false },

            };

            if (!string.IsNullOrEmpty(mainCar.MotorType))
            {
                for (int i = 0; i < MotorType.Count; i++)
                {
                    if (MotorType[i].Name.Equals(mainCar.MotorType)) MotorType[i].IsSelected = true;
                 }

            }
         }

Main Class where I want to write the value

public class MainCar: INotifyPropertyChanged
        {  string motortype;
            public string MotorType
            {
                set
                {
                    if (motortype != value)
                    {
                        motortype = value;
                        OnPropertyChanged("MotorType");
    
                    }
                }
                get
                {
                    return motortype;
                }
    
            }
            public event PropertyChangedEventHandler PropertyChanged;
    
            protected virtual void OnPropertyChanged(string propertyName)
            {
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }
    }

But If I write this code in listview,I do not have error,but I need collection view to make 3 columns of the list item`s

My previous question(this) was closed because someone give this link

What is a NullReferenceException, and how do I fix it?

I know what is NullReferenceException.I don't understand why this code works when I write ListView and does not work in collectionview Please do not close my question again and help me

0 Answers0