Faraday
Faraday

Reputation: 2954

OnPropertyChanged triggering Null exception

I have a dataset which is made up of an array of Solutions.

Each solution has A,B and C fields.

I set my DataSource like this:

DataSource = GetDataSource();


private Array _dataSource;

public virtual Array DataSource
{
    get { return _dataSource; }
    set
    {
        if (value != null && value.Length == 0)
        {
            return;
        }
        _dataSource = value;
        OnPropertyChanged("HasData");
        OnPropertyChanged("DataSource");
    }
}

The XAML for my chart is simple:

<dxc:ChartControl x:Name="ChartControl" 
                  DataSource="{Binding DataSource}" 
                  CrosshairEnabled="True" ToolTipEnabled="False">
    <dxc:ChartControl.Diagram>
        <dxc:XYDiagram2D SeriesDataMember="A" Rotated="True">
            <dxc:XYDiagram2D.SeriesTemplate>
                <dxc:PointSeries2D ArgumentDataMember="B" ValueDataMember="C"
                                   ArgumentScaleType="Auto" ValueScaleType="Auto">
                </dxc:PointSeries2D>
            </dxc:XYDiagram2D.SeriesTemplate>
        </dxc:XYDiagram2D>
    </dxc:ChartControl.Diagram>
</dxc:ChartControl>

The problem is, when I set my DataSource, and call OnPropertyChanged("DataSource");, I get a null reference exception. Now, "DataSource" isn't actually null, and when I comment out my XAML for my chart it works, so I'm assuming/hoping I'm just doing something silly with my chart.

Note: A,B & C are all Doubles.

Edit** Adding StackTrace

   at DevExpress.Charts.Native.RefinedSeriesGroup.UpdateTransformation()
   at DevExpress.Charts.Native.RefinedSeriesGroupController.UpdateAxisData(IEnumerable`1 axes, IMinMaxValuesCalculator minMaxCalculator)
   at DevExpress.Charts.Native.SeriesController.UpdateRange(ChartUpdateAggregator updateAggregator)
   at DevExpress.Charts.Native.SeriesController.ApplyUpdates(ChartUpdateAggregator updateAggregator, Boolean pointsUpdated, Boolean isContainsProcessedPointsBeforeUpdate)
   at DevExpress.Charts.Native.SeriesController.SeriesControllerTransaction.Commit()
   at DevExpress.Charts.Native.SeriesController.CommitTransaction()
   at DevExpress.Xpf.Charts.Native.ViewController.ProcessChanged(ChartUpdate update)
   at DevExpress.Xpf.Charts.Native.ViewController.EndUpdateData()
   at DevExpress.Xpf.Charts.Diagram.UpdateBinding()
   at DevExpress.Xpf.Charts.ChartControl.<.ctor>b__0()
   at DevExpress.Xpf.Charts.Native.BindingBehavior.UpdateActualDataSource(Object newDataSource)
   at DevExpress.Xpf.Charts.ChartControl.DataSourcePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
   at System.Windows.DependencyObject.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
   at System.Windows.FrameworkElement.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
   at System.Windows.DependencyObject.NotifyPropertyChange(DependencyPropertyChangedEventArgs args)
   at System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex entryIndex, DependencyProperty dp, PropertyMetadata metadata, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType)
   at System.Windows.DependencyObject.InvalidateProperty(DependencyProperty dp, Boolean preserveCurrentValue)
   at System.Windows.Data.BindingExpressionBase.Invalidate(Boolean isASubPropertyChange)
   at System.Windows.Data.BindingExpression.TransferValue(Object newValue, Boolean isASubPropertyChange)
   at System.Windows.Data.BindingExpression.ScheduleTransfer(Boolean isASubPropertyChange)
   at MS.Internal.Data.ClrBindingWorker.NewValueAvailable(Boolean dependencySourcesChanged, Boolean initialValue, Boolean isASubPropertyChange)
   at MS.Internal.Data.PropertyPathWorker.UpdateSourceValueState(Int32 k, ICollectionView collectionView, Object newValue, Boolean isASubPropertyChange)
   at MS.Internal.Data.ClrBindingWorker.OnSourcePropertyChanged(Object o, String propName)
   at MS.Internal.Data.PropertyPathWorker.OnPropertyChanged(Object sender, PropertyChangedEventArgs e)
   at System.Windows.WeakEventManager.ListenerList`1.DeliverEvent(Object sender, EventArgs e, Type managerType)
   at System.ComponentModel.PropertyChangedEventManager.OnPropertyChanged(Object sender, PropertyChangedEventArgs args)
   at Microsoft.Practices.Prism.Mvvm.BindableBase.OnPropertyChanged(String propertyName)
   at KOP_Report_Generator.ViewModel.DataSourceViewModelBase.set_DataSource(Array value) in c:\Users\a-vishar\Documents\Visual Studio 2012\Projects\KOP Report Generator\KOP Report Generator\ViewModel\DataSourceViewModelBase.cs:line 27
   at KOP_Report_Generator.ViewModel.ExampleViewModel.Refresh(DataTable datatable) in c:\Users\a-vishar\Documents\Visual Studio 2012\Projects\KOP Report Generator\KOP Report Generator\ViewModel\ExampleViewModel.cs:line 28
   at KOP_Report_Generator.ViewModel.MainViewModel.<>c__DisplayClass1.<GenerateCharts>b__0(IDataSourceViewModel m) in c:\Users\a-vishar\Documents\Visual Studio 2012\Projects\KOP Report Generator\KOP Report Generator\ViewModel\MainViewModel.cs:line 133
   at Extensions.CollectionExtensions.ForEach[T](IEnumerable`1 collection, Action`1 action) in c:\Users\a-vishar\Documents\Visual Studio 2012\Projects\KOP Report Generator\Extensions\CollectionExtensions.cs:line 125
   at KOP_Report_Generator.ViewModel.MainViewModel.GenerateCharts() in c:\Users\a-vishar\Documents\Visual Studio 2012\Projects\KOP Report Generator\KOP Report Generator\ViewModel\MainViewModel.cs:line 133
   at KOP_Report_Generator.View.Tabs.HomeTabView.GenerateCharts(Object sender, RoutedEventArgs e) in c:\Users\a-vishar\Documents\Visual Studio 2012\Projects\KOP Report Generator\KOP Report Generator\View\Tabs\HomeTabView.xaml.cs:line 34
   at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
   at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
   at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
   at System.Windows.UIElement.RaiseEvent(RoutedEventArgs e)
   at System.Windows.Controls.Primitives.ButtonBase.OnClick()
   at System.Windows.Controls.Button.OnClick()
   at System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(MouseButtonEventArgs e)
   at System.Windows.UIElement.OnMouseLeftButtonUpThunk(Object sender, MouseButtonEventArgs e)
   at System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
   at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
   at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
   at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
   at System.Windows.UIElement.ReRaiseEventAs(DependencyObject sender, RoutedEventArgs args, RoutedEvent newEvent)
   at System.Windows.UIElement.OnMouseUpThunk(Object sender, MouseButtonEventArgs e)
   at System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
   at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
   at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
   at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
   at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
   at System.Windows.UIElement.RaiseTrustedEvent(RoutedEventArgs args)
   at System.Windows.UIElement.RaiseEvent(RoutedEventArgs args, Boolean trusted)
   at System.Windows.Input.InputManager.ProcessStagingArea()
   at System.Windows.Input.InputManager.ProcessInput(InputEventArgs input)
   at System.Windows.Input.InputProviderSite.ReportInput(InputReport inputReport)
   at System.Windows.Interop.HwndMouseInputProvider.ReportInput(IntPtr hwnd, InputMode mode, Int32 timestamp, RawMouseActions actions, Int32 x, Int32 y, Int32 wheel)
   at System.Windows.Interop.HwndMouseInputProvider.FilterMessage(IntPtr hwnd, WindowMessage msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   at System.Windows.Interop.HwndSource.InputFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
   at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
   at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
   at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
   at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
   at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
   at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
   at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
   at System.Windows.Threading.Dispatcher.Run()
   at System.Windows.Application.RunDispatcher(Object ignore)
   at System.Windows.Application.RunInternal(Window window)
   at System.Windows.Application.Run(Window window)
   at System.Windows.Application.Run()
   at KOP_Report_Generator.App.Main() in c:\Users\a-vishar\Documents\Visual Studio 2012\Projects\KOP Report Generator\KOP Report Generator\obj\Debug\App.g.cs:line 50
   at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
   at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()

Note** The issue goes away if I pick ScaleTypes of Numerical instead of Auto...

Upvotes: 0

Views: 518

Answers (1)

Jashaszun
Jashaszun

Reputation: 9270

An event that has no subscribers is null. The problem is probably that your event has no subscribers. So you need to check that your event is not null before invoking it:

if (OnPropertyChanged != null)
{
    OnPropertyChanged("HasData");
    OnPropertyChanged("DataSource");
}

Upvotes: 1

Related Questions