<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>

    <meta http-equiv="content-type" content="text/html; charset=ISO-8859-2">
  </head>
  <body bgcolor="#ffffff" text="#000000">
    Hi all,<br>
    I've found a bug in IronPython 2.6.1 that causes to terminate
    IronPython 2.6.1 based on .NET 2.0 with <tt>SystemError: Object
      reference not set to an instance of an object.</tt> exception.<br>
    <br>
    Below is the code. You need clrtype.py and pyevent.py to run it.
    When you run it, a simple form with listbox and checkbox appears.
    The SK items in the listbox can be filtered out by checking the
    checkbox. There is a binding to the selected item in the listbox
    which is the cause of the error. If you select the SK item and check
    the checkbox to filter it out, the exception occurs. But it only
    occurs in IronPython based on .NET 2.0 - it works fine in IronPython
    2.6.1 based on .NET 4.<br>
    <br>
    Here is the full exception:<br>
    <br>
    <tt>C:\BindingTest&gt;C:\IronPython-2.6.1.NET20\ipy.exe -X:Debug
      -X:ShowClrExceptions BindingTest.py<br>
      Traceback (most recent call last):<br>
        File "BindingTest.py", line 133, in &lt;module&gt;<br>
        File "BindingTest.py", line 125, in filtered<br>
      SystemError: Object reference not set to an instance of an object.<br>
      CLR Exception:<br>
          NullReferenceException<br>
      :<br>
      Object reference not set to an instance of an object.<br>
    </tt><br>
    I'm going to build IronPython in the debug mode this afternoon to
    see what's wrong inside and possible fix it in my own build.<br>
    <br>
    As this is quite showstopper for me, I would be very happy if
    somebody can help me with this. Thank you.<br>
    <br>
    --<br>
    -- Lukáš<br>
    <br>
    --- file BindingTest.py ---<br>
    <br>
    <tt>import clr<br>
      import clrtype<br>
      import pyevent<br>
      clr.AddReference('WindowsBase')<br>
      clr.AddReference('PresentationFramework')<br>
      from System.Windows import Application<br>
      from System.ComponentModel import INotifyPropertyChanged,
      PropertyChangedEventArgs<br>
      from System.Collections.ObjectModel import ObservableCollection<br>
      from System.Windows.Markup import XamlReader<br>
      from System.Windows.Data import CollectionViewSource<br>
      <br>
      xaml = """&lt;Window
      xmlns=<a class="moz-txt-link-rfc2396E" href="http://schemas.microsoft.com/winfx/2006/xaml/presentation">"http://schemas.microsoft.com/winfx/2006/xaml/presentation"</a><br>
        xmlns:x=<a class="moz-txt-link-rfc2396E" href="http://schemas.microsoft.com/winfx/2006/xaml">"http://schemas.microsoft.com/winfx/2006/xaml"</a><br>
        Title="Binding test" Height="150" Width="250"&gt;<br>
        &lt;Grid x:Name="root"&gt;<br>
          &lt;Grid.RowDefinitions&gt;<br>
            &lt;RowDefinition Height="Auto" /&gt;<br>
            &lt;RowDefinition Height="*" /&gt;<br>
            &lt;RowDefinition Height="Auto" /&gt;<br>
          &lt;/Grid.RowDefinitions&gt;<br>
          &lt;CheckBox Content="Filtered" IsChecked="{Binding filtered}"
      /&gt;<br>
          &lt;ListBox Name="lbxMountains" Grid.Row="1"<br>
                    ItemsSource="{Binding lstMountains}"<br>
                    SelectedItem="{Binding selMountain}"&gt;<br>
            &lt;ListBox.ItemTemplate&gt;<br>
              &lt;DataTemplate&gt;<br>
                &lt;StackPanel Orientation="Horizontal"&gt;<br>
                  &lt;TextBlock Grid.Column="1" Text="{Binding country}"
      /&gt;<br>
                  &lt;TextBlock Text=": " /&gt;<br>
                  &lt;TextBlock Text="{Binding name}" /&gt;<br>
                &lt;/StackPanel&gt;<br>
              &lt;/DataTemplate&gt;<br>
            &lt;/ListBox.ItemTemplate&gt;<br>
          &lt;/ListBox&gt;<br>
          &lt;TextBlock Grid.Row="2" Text="{Binding selMountain.name}"
      /&gt;<br>
        &lt;/Grid&gt;<br>
      &lt;/Window&gt;"""<br>
      <br>
      class NotifyPropertyChangedBase(INotifyPropertyChanged):<br>
          PropertyChanged = None<br>
      <br>
          def __init__(self):<br>
              self.PropertyChanged, self._propertyChangedCaller =
      pyevent.make_event()<br>
      <br>
          def add_PropertyChanged(self, value):<br>
              self.PropertyChanged += value<br>
      <br>
          def remove_PropertyChanged(self, value):<br>
              self.PropertyChanged -= value<br>
      <br>
          def OnPropertyChanged(self, propertyName):<br>
              if self.PropertyChanged is not None:<br>
                  self._propertyChangedCaller(self,
      PropertyChangedEventArgs(propertyName))<br>
      <br>
      class Mountain(NotifyPropertyChangedBase):<br>
          __metaclass__ = clrtype.ClrClass<br>
      <br>
          def __init__(self, name, country):<br>
              super(Mountain, self).__init__()<br>
              self.name = name<br>
              self.country = country<br>
      <br>
          @property<br>
          def name(self):<br>
              return self._name<br>
      <br>
          @name.setter<br>
          def name(self, value):<br>
              self._name = value<br>
              self.OnPropertyChanged('name')<br>
      <br>
          @property<br>
          def country(self):<br>
              return self._country<br>
      <br>
          @country.setter<br>
          def country(self, value):<br>
              self._country = value<br>
              self.OnPropertyChanged('country')<br>
      <br>
      class Mountains(NotifyPropertyChangedBase):<br>
          __metaclass__ = clrtype.ClrClass<br>
      <br>
          def __init__(self):<br>
              super(Mountains, self).__init__()<br>
              self._lstMountains = ObservableCollection[Mountain]()<br>
              self.lstMountains.Add(Mountain('Snezka', 'CR'))<br>
              self.lstMountains.Add(Mountain('Rip', 'CR'))<br>
              self.lstMountains.Add(Mountain('Gerlach', 'SK'))<br>
              self.filtered = False<br>
              self.selMountain = Mountain('', '')<br>
      <br>
          @property<br>
          @clrtype.accepts()<br>
          @clrtype.returns(Mountain)<br>
          def selMountain(self):<br>
              return self._selMountain<br>
      <br>
          @selMountain.setter<br>
          @clrtype.accepts(Mountain)<br>
          @clrtype.returns()<br>
          def selMountain(self, value):<br>
              self._selMountain = value<br>
              self.OnPropertyChanged('selMountain')<br>
      <br>
          @property<br>
          @clrtype.accepts()<br>
          @clrtype.returns(ObservableCollection[Mountain])<br>
          def lstMountains(self):<br>
              return self._lstMountains<br>
      <br>
          @property<br>
          @clrtype.accepts()<br>
          @clrtype.returns(bool)<br>
          def filtered(self):<br>
              return self._filtered<br>
      <br>
          @filtered.setter<br>
          @clrtype.accepts(bool)<br>
          @clrtype.returns()<br>
          def filtered(self, value):<br>
              self._filtered = value<br>
              self.OnPropertyChanged('filtered')<br>
              dv =
      CollectionViewSource.GetDefaultView(self.lstMountains)<br>
              dv.Filter = (lambda x: x.country == 'CR' if value else
      True)<br>
      <br>
      class MyApp:<br>
          def __init__(self):<br>
              self.root = XamlReader.Parse(xaml)<br>
              self.root.DataContext = Mountains()<br>
      <br>
      app = Application()<br>
      app.Run(MyApp().root)</tt><br>
  </body>
</html>