Consider following XAML in Xamarin Forms. I have binded a Picker control with an ObserableCollection and assigned a MultiBinding to its ItemDisplayBinding property -
<Picker ItemsSource="{Binding BooksList}">
<Picker.ItemDisplayBinding>
<MultiBinding StringFormat="{}{0} - {1}">
<Binding Path="Title"/>
<Binding Path="Subtitle"/>
</MultiBinding>
</Picker.ItemDisplayBinding>
</Picker>
<Button Text="Add" Command="{Binding AddCommand}"/>
<Button Text="Remove" Command="{Binding RemoveCommand}"/>
And the corresponding ViewModel -
AddCommand = new Command((object item) =>
{
BooksList.Add(new ListItem
{
Title = "Book Name",
Subtitle = "Author Name"
});
}
...
private ObservableCollection<ListItem> _booksList;
public ObservableCollection<ListItem> BooksList
{
get => _booksList;
set
{
_booksList = value;
OnPropertyChanged(nameof(BooksList));
}
}
...
public BookClass
{
BooksList = new ObservableCollection<ListItem>();
}
...
public class ListItem
{
public string Title { get; set; }
public string Subtitle { get; set; }
}
Now, when I run the application and hit the Add button ONCE it works fine and I can see an element in the picker control. However, when I hit the Add button again, it throws following exception -
Object reference not set to an instance of an object
Stack trace -
{System.NullReferenceException: Object reference not set to an instance of an object.
at Xamarin.Forms.MultiBinding.Apply (System.Object context, Xamarin.Forms.BindableObject targetObject, Xamarin.Forms.BindableProperty targetProperty, System.Boolean fromBindingContextChanged) [0x0013a] in D:\a\1\s\Xamarin.Forms.Core\MultiBinding.cs:147
at Xamarin.Forms.Picker.GetDisplayMember (System.Object item) [0x00018] in D:\a\1\s\Xamarin.Forms.Core\Picker.cs:184
at Xamarin.Forms.Picker.AddItems (System.Collections.Specialized.NotifyCollectionChangedEventArgs e) [0x00032] in D:\a\1\s\Xamarin.Forms.Core\Picker.cs:252
at Xamarin.Forms.Picker.CollectionChanged (System.Object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) [0x00010] in D:\a\1\s\Xamarin.Forms.Core\Picker.cs:238
at System.Collections.ObjectModel.ObservableCollection`1[T].OnCollectionChanged (System.Collections.Specialized.NotifyCollectionChangedEventArgs e) [0x00018] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/external/corefx/src/System.ObjectModel/src/System/Collections/ObjectModel/ObservableCollection.cs:263
at System.Collections.ObjectModel.ObservableCollection`1[T].OnCollectionChanged (System.Collections.Specialized.NotifyCollectionChangedAction action, System.Object item, System.Int32 index) [0x00000] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/external/corefx/src/System.ObjectModel/src/System/Collections/ObjectModel/ObservableCollection.cs:338
at System.Collections.ObjectModel.ObservableCollection`1[T].InsertItem (System.Int32 index, T item) [0x0001a] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/external/corefx/src/System.ObjectModel/src/System/Collections/ObjectModel/ObservableCollection.cs:196
at System.Collections.ObjectModel.Collection`1[T].Add (T item) [0x00020] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/external/corefx/src/Common/src/CoreLib/System/Collections/ObjectModel/Collection.cs:71
at RS.Apps.ViewModels.BookShelf.BookShelfViewModel.<.ctor>b__37_3 (System.Object item) [0x00018] in C:\user43424\GitHub\Booking\Mobile\Apps\ViewModels\BookShelf\BookShelfViewModel.cs:97 }
I am unable to wrap my hand around in what's going on here. Why the null reference exception, what am I missing?