Bug 16902 - Unhandled NullReferenceException in System.ComponentModel.BindingList<T>
Summary: Unhandled NullReferenceException in System.ComponentModel.BindingList<T>
Status: RESOLVED FIXED
Alias: None
Product: Class Libraries
Classification: Mono
Component: System ()
Version: 3.2.x
Hardware: All All
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2013-12-19 18:26 UTC by Scott McArthur
Modified: 2014-12-09 08:24 UTC (History)
2 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED FIXED

Description Scott McArthur 2013-12-19 18:26:45 UTC
Description of Problem:
Unhandled NullReferenceException in System.ComponentModel.BindingList<T>. 


Steps to reproduce the problem:
1. Create a class the implements an INotifyPropertyChanged

public class Foo : INotifyPropertyChanged
{
  private string _name;

  public event PropertyChangedEventHandler PropertyChanged;

  public string Name 
  {
    get { return _name; }
    set
    {
      _name = value;

      if (PropertyChanged != null)
      {
        PropertyChanged(this, new PropertyChangedEventArgs("Name"));
      }
    }
  }
}

2. Create a BindList of its type:

var list = new BindingList<Foo>();
list.Insert(0, null);
Console.WriteLine(list.Count);


Actual Results:
`NullReferenceException` thrown on Insert

Expected Results:
Item added to collection, without exception


How often does this happen?
All the time


Additional Information:
https://github.com/mono/mono/blob/master/mcs/class/System/System.ComponentModel/BindingList.cs
A check on line #246 (item as INotifyPropertyChanged).PropertyChanged += Item_PropertyChanged;
should be made before casting item, as it may be null.
Comment 1 Scott McArthur 2013-12-20 05:51:47 UTC
Created pull request for fix. https://github.com/mono/mono/pull/842
Comment 2 Marek Safar 2014-12-09 08:24:26 UTC
Already fixed in mono 3.10