OnPropertyChanged触发Null异常

本文关键字:异常 Null 触发 OnPropertyChanged | 更新日期: 2023-09-27 18:16:04

我有一个由Solution s数组组成的数据集。

每个解决方案都有A、B和C字段。

我这样设置我的数据源:

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");
    }
}

我的图表的XAML很简单:

<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>

问题是,当我设置DataSource并调用OnPropertyChanged("DataSource");时,我会得到一个null引用异常。现在,"DataSource"实际上并不是null,当我为我的图表注释XAML时,它是有效的,所以我假设/希望我只是在对我的图表做一些愚蠢的事情。

注:A、B&C都是双打。

编辑**添加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()

注**如果我选择Numerical的ScaleTypes而不是Auto,问题就会消失。。。

OnPropertyChanged触发Null异常

没有订阅者的eventnull。问题可能是您的event没有订阅者。因此,在调用它之前,您需要检查您的event不是null

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