Binding to a Silverlight Chart Series gives a null reference exception

This just wasted a good part of my day. I wanted to use a Silverlight Toolkit chart, and I wanted to use declarative databinding for the ColumnSeries (although LineSeries would fail similarly).

I have a DependencyObject as my datasource (although the same would probably happen with a POCO or an INotifyPropertyChanged object) with various dependency properties I want to bind. Here’s the kind of declaration that gives the error:


<chart:Chart x:Name="chart"
 Height="500">
 <chart:Chart.Series>
 <chart:ColumnSeries ItemsSource="{Binding Results, Mode=OneWay}"
 IndependentValuePath="GenreTitle" />
 </chart:Chart.Series>
 </chart:Chart>

When the chart is created, I get a System.NullReferenceException when I set the value of Results.

At first, I thought my Results enumeration was somehow wrong, but the same data exactly worked perfectly in a ListBox and a DataGrid, so the data was probably OK.

It was simpler than that, though – I’d missed out the DependentValuePath. You need to specify both otherwise you (rightly) get a null reference exception.

Simple, but it took me a bit of puzzling to find it.

About these ads

11 comments

    1. Well, that’s a slightly simpler scenario – you’re binding to a collection of simple types, so the chart can do something sensible by default. The exception comes when you’re binding to a collection of more complex objects, and the chart has no idea which member to use as the dependent value. In my case, the object I’m binding to has tons of properties amd I’d just forgotten to tell it where to get the label and value properties.

  1. If got a similar problem.

    Silverlight Toolkit Chart with 2 series using ObservableCollection. The datatype bound implements notification. Collections are bound to chart, then values are retrieved from an web service and added. I can’t get rid of this error.

    I have no luck since i’m binding just one ObservableCollection with objects like this:

    class ChartData {
    public DateTime date; //independent value
    public int data1; // dependent, used in series 1
    public int data2; // dependent, used in series 2
    }

    It worked well using two ObservableCollections.

    class ChartData {
    public DateTime date;
    public int data;
    }

    xaml like this

    <chartingToolkit:ColumnSeries ItemsSource="{Binding Mode=OneWay}"
    DependentValuePath="Bars"
    IndependentValuePath="Date"

  2. ups, xaml code imcomplete

    <chartingToolkit:ColumnSeries ItemsSource="{Binding Mode=OneWay}"
    DependentValuePath="Bars"
    IndependentValuePath="Date"

    Code behind:


    items = new ObservableCollection();

    ((AreaSeries)Chart.Series[0]).ItemsSource = items;
    ((ColumnSeries)Chart.Series[1]).ItemsSource = items;

    1. It works fine for me when I use the generic ObservableCollection like this:

      ObservableCollection coll = new ObservableCollection();
      coll.Add(new ChartItem(DateTime.Parse(“21 dec 2009″), 5,7));
      coll.Add(new ChartItem(DateTime.Parse(“22 dec 2009″), 4, 14));
      coll.Add(new ChartItem(DateTime.Parse(“23 dec 2009″), 12, 6));
      coll.Add(new ChartItem(DateTime.Parse(“24 dec 2009″), 17, 18));
      coll.Add(new ChartItem(DateTime.Parse(“25 dec 2009″), 11, 5));
      //chart1.DataContext = coll;
      ((ColumnSeries)chart1.Series[0]).ItemsSource = coll;
      ((ColumnSeries)chart1.Series[1]).ItemsSource = coll;

      And the following Xaml:


      <chartingToolkit:Chart Name="chart1" Title="Chart Title">
      <chartingToolkit:ColumnSeries DependentValuePath="data1" IndependentValuePath="date" ItemsSource="{Binding}" />
      <chartingToolkit:ColumnSeries DependentValuePath="data2" IndependentValuePath="date" ItemsSource="{Binding}" />
      </chartingToolkit:Chart>

      1. In a simple setup, I have no problems binding an empty ObservableCollection two both series and adding values later using a timer.

        But adding values later in the app I have to build gives me this NullReference Exception, no matter what I try to do. No luck so far.

      2. When I retrive data from the web service, everything is fine until I start to add the data to the ObservableColletion which is bound to the chart.

        I started to use a simple data class without notification to prepare data, then build the data objects I’m using in the chart (which are notify on change), add them to the Collection. Add to the collection gives the error and adding them to another Collection which is not bound and change the binding of the chart raises the error, too.

  3.    at System.Windows.Controls.DataVisualization.Charting.ColumnSeries.c__DisplayClass8.b__4()
       at System.Windows.Controls.DataVisualization.Charting.DataPointSeriesWithAxes.GetAxes(DataPoint firstDataPoint, Func`2 independentAxisPredicate, Func`1 independentAxisFactory, Func`2 dependentAxisPredicate, Func`1 dependentAxisFactory)
       at System.Windows.Controls.DataVisualization.Charting.ColumnSeries.GetAxes(DataPoint firstDataPoint)
       at System.Windows.Controls.DataVisualization.Charting.DataPointSeriesWithAxes.GetAxes()
       at System.Windows.Controls.DataVisualization.Charting.DataPointSeriesWithAxes.OnDataPointsChanged(IList`1 newDataPoints, IList`1 oldDataPoints)
       at System.Windows.Controls.DataVisualization.Charting.DataPointSingleSeriesWithAxes.OnDataPointsChanged(IList`1 newDataPoints, IList`1 oldDataPoints)
       at System.Windows.Controls.DataVisualization.Charting.ColumnBarBaseSeries`1.OnDataPointsChanged(IList`1 newDataPoints, IList`1 oldDataPoints)
       at System.Windows.Controls.DataVisualization.Charting.DataPointSeries.LoadDataPoints(IEnumerable newItems, IEnumerable oldItems)
       at System.Windows.Controls.DataVisualization.Charting.DataPointSeries.OnItemsSourceCollectionChanged(IEnumerable collection, NotifyCollectionChangedEventArgs e)
       at System.Windows.Controls.DataVisualization.Charting.DataPointSeries.ItemsSourceCollectionChanged(Object sender, NotifyCollectionChangedEventArgs e)
       at System.Windows.Controls.DataVisualization.Charting.DataPointSeries.b__0(DataPointSeries instance, Object source, NotifyCollectionChangedEventArgs eventArgs)
       at System.Windows.Controls.WeakEventListener`3.OnEvent(TSource source, TEventArgs eventArgs)
       at System.Collections.Specialized.NotifyCollectionChangedEventHandler.Invoke(Object sender, NotifyCollectionChangedEventArgs e)
       at System.Collections.ObjectModel.ObservableCollection`1.OnCollectionChanged(NotifyCollectionChangedEventArgs e)
       at System.Collections.ObjectModel.ObservableCollection`1.InsertItem(Int32 index, T item)
       at System.Collections.ObjectModel.Collection`1.Add(T item)
       at MedIntake.ColumnInputChart.c__DisplayClass2.b__0()
    
    
    I had no luck including the silverlight toolkit source code into my project to be able to step into toolkit code and see what happens there.
    1. stacktrace lacks some ‘tags’. should look like this

      at MedIntake.ColumnInputChart.<>c_ass2.<get_OnComplete>b__0()_DisplayCl
      

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s