This is a migrated thread and some comments may be shown as answers.

NullReferenceException when adding a Bollinger Band indicator to the chart

5 Answers 106 Views
ChartView
This is a migrated thread and some comments may be shown as answers.
Dev
Top achievements
Rank 1
Dev asked on 16 Nov 2012, 02:03 AM

Hi,

I'm using Telerik RadControls Q3 2012. After the update, I plan to add a Bollinger Band to a ChartView. However, after several trials, it always hangs the application by System.NullReferenceException: Object reference not set to an instance of an object.

The ChartView I'm using is a simple LineSeries chart in XAML:

<telerik:RadCartesianChart Margin="5" Grid.Column="0" x:Name="PriceChart" Padding="0,0,0,0">
                    <telerik:RadCartesianChart.HorizontalAxis>
                        <telerik:DateTimeContinuousAxis LabelFormat="d-MMM-yy"
                                                                MajorStep="1"
                                                                MajorStepUnit="Month"                                                               
                                                                GapLength="0.8"
                                                                PlotMode="OnTicks"                                                               
                                                                />
                    </telerik:RadCartesianChart.HorizontalAxis>
                    <telerik:RadCartesianChart.VerticalAxis>
                        <telerik:LinearAxis BorderBrush="DimGray" BorderThickness="0.5" />
                    </telerik:RadCartesianChart.VerticalAxis>
                    <telerik:RadCartesianChart.Series>
                        <chartView:LineSeries ItemsSource="{Binding ChartData}"
                                              CategoryBinding="Date"
                                              ValueBinding="Price" 
                                              Stroke="Black"
                                              StrokeThickness="0.5"/>                       
                    </telerik:RadCartesianChart.Series>
                    <telerik:RadCartesianChart.Grid>
                        <telerik:CartesianChartGrid MajorLinesVisibility="Y"/>
                    </telerik:RadCartesianChart.Grid>
                </telerik:RadCartesianChart>




This is how I add the Bollinger Band to the chart:

 

LineSeries lineSeries = this.PriceChart.Series[0] as LineSeries;
                if ((lineSeries != null) && (lineSeries.ItemsSource != null))
                {
                    BollingerBandsIndicator indicator = new BollingerBandsIndicator();
                    indicator.Period = 5;
                    indicator.StandardDeviations = 2;
                    indicator.CategoryBinding = lineSeries.CategoryBinding;
                    indicator.ValueBinding = lineSeries.ValueBinding;
                    indicator.ItemsSource = lineSeries.ItemsSource;
 
                    indicator.Stroke = Brushes.Red;
                    indicator.LowerBandStroke = Brushes.Red;
                    indicator.StrokeThickness = 1;
 
                    this.PriceChart.Indicators.Add(indicator);
                }

 

 

 



And this is the StackTrace for the Exception caught:
  

at
Telerik.Windows.Controls.ChartView.BollingerBandsIndicatorDataSource.BindCore()
in
c:\TB\105\WPF_Scrum\Release_WPF\Sources\Development\Controls\Chart\Visualization\DataBinding\DataSources\Financial\BollingerBandsIndicatorDataSource.cs:line
63<BR>   at
Telerik.Windows.Controls.ChartView.ChartSeriesDataSource.Bind() in
c:\TB\105\WPF_Scrum\Release_WPF\Sources\Development\Controls\Chart\Visualization\DataBinding\DataSources\ChartSeriesDataSource.cs:line
235<BR>   at
Telerik.Windows.Controls.ChartView.ChartSeriesDataSource.Rebind(Boolean
itemsSourceChanged, IEnumerable newSource) in
c:\TB\105\WPF_Scrum\Release_WPF\Sources\Development\Controls\Chart\Visualization\DataBinding\DataSources\ChartSeriesDataSource.cs:line
81<BR>   at
Telerik.Windows.Controls.ChartView.ChartSeriesDataSource.set_ItemsSource(IEnumerable
value) in
c:\TB\105\WPF_Scrum\Release_WPF\Sources\Development\Controls\Chart\Visualization\DataBinding\DataSources\ChartSeriesDataSource.cs:line
54<BR>   at
Telerik.Windows.Controls.ChartView.ChartSeries.InitDataBinding() in
c:\TB\105\WPF_Scrum\Release_WPF\Sources\Development\Controls\Chart\Visualization\Common\ChartSeries.cs:line
316<BR>   at
Telerik.Windows.Controls.ChartView.IndicatorBase.InitDataBinding() in
c:\TB\105\WPF_Scrum\Release_WPF\Sources\Development\Controls\Chart\Visualization\CartesianChart\Indicators\IndicatorBase.cs:line
94<BR>   at
Telerik.Windows.Controls.ChartView.ValueIndicatorBase.InitDataBinding() in
c:\TB\105\WPF_Scrum\Release_WPF\Sources\Development\Controls\Chart\Visualization\CartesianChart\Indicators\ValueIndicatorBase.cs:line
50<BR>   at
Telerik.Windows.Controls.ChartView.ChartSeries.OnApplyTemplate() in
c:\TB\105\WPF_Scrum\Release_WPF\Sources\Development\Controls\Chart\Visualization\Common\ChartSeries.cs:line
239<BR>   at
Telerik.Windows.Controls.ChartView.LineIndicatorBase.OnApplyTemplate() in
c:\TB\105\WPF_Scrum\Release_WPF\Sources\Development\Controls\Chart\Visualization\CartesianChart\Indicators\LineIndicatorBase.cs:line
112<BR>   at
Telerik.Windows.Controls.ChartView.BollingerBandsIndicator.OnApplyTemplate() in
c:\TB\105\WPF_Scrum\Release_WPF\Sources\Development\Controls\Chart\Visualization\CartesianChart\Indicators\BollingerBandsIndicator.cs:line
125<BR>   at
System.Windows.FrameworkElement.ApplyTemplate()<BR>   at
Telerik.Windows.Controls.ChartView.RadChartBase.OnPresenterAdded(ChartElementPresenter
presenter) in
c:\TB\105\WPF_Scrum\Release_WPF\Sources\Development\Controls\Chart\Visualization\RadChartBase.cs:line
405<BR>   at
Telerik.Windows.Controls.ChartView.PresenterCollection`1.InsertItem(Int32 index,
T item) in
c:\TB\105\WPF_Scrum\Release_WPF\Sources\Development\Controls\Chart\Visualization\Common\PresenterCollection.cs:line
34<BR>   at
STA.Modules.MarketData.Views.PairAnalytics.PairAnalyticsDetailPairView.<PostQueryChartData>b__0()
in C:\Segantii\Development\Source Control\Projects\Odin\trunk\Source
Codes\SegantiiTradingAnalytics\STA.Modules.MarketData\Views\PairAnalytics\PairAnalyticsDetailPairView.xaml.cs:line
70<BR>   at
System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback,
Object args, Int32 numArgs)<BR>   at
MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate
method, Object args, Int32 numArgs, Delegate catchHandler)<BR>   at
System.Windows.Threading.Dispatcher.WrappedInvoke(Delegate callback, Object
args, Int32 numArgs, Delegate catchHandler)<BR>   at
System.Windows.Threading.DispatcherOperation.InvokeImpl()<BR>   at
System.Threading.ExecutionContext.runTryCode(Object userData)<BR>   at
System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode
code, CleanupCode backoutCode, Object userData)<BR>   at
System.Threading.ExecutionContext.Run(ExecutionContext executionContext,
ContextCallback callback, Object state, Boolean ignoreSyncCtx)<BR>  
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext,
ContextCallback callback, Object state)<BR>   at
System.Windows.Threading.DispatcherOperation.Invoke()<BR>   at
System.Windows.Threading.Dispatcher.ProcessQueue()<BR>   at
System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd, Int32 msg, IntPtr
wParam, IntPtr lParam, Boolean& handled)<BR>   at
MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr
lParam, Boolean& handled)<BR>   at
MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)<BR>   at
System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback,
Object args, Int32 numArgs)<BR>   at
MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate
method, Object args, Int32 numArgs, Delegate catchHandler)<BR>   at
System.Windows.Threading.Dispatcher.WrappedInvoke(Delegate callback, Object
args, Int32 numArgs, Delegate catchHandler)<BR>   at
System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority priority,
TimeSpan timeout, Delegate method, Object args, Int32 numArgs)<BR>  
at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam,
IntPtr lParam)<BR>   at
MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)<BR>   at
System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame
frame)<BR>   at System.Windows.Application.RunInternal(Window
window)<BR>   at System.Windows.Application.Run()<BR>   at
SegantiiTradingAnalytics.App.Main() in C:\Segantii\Development\Source
Control\Projects\Odin\trunk\Source
Codes\SegantiiTradingAnalytics\SegantiiTradingAnalytics\obj\x86\Debug\App.g.cs:line
0<BR>   at System.AppDomain._nExecuteAssembly(RuntimeAssembly
assembly, String[] args)<BR>   at
System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext
activationContext, String[] activationCustomData)<BR>   at
Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssemblyDebugInZone()<BR>  
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext,
ContextCallback callback, Object state, Boolean ignoreSyncCtx)<BR>  
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext,
ContextCallback callback, Object state)<BR>   at
System.Threading.ThreadHelper.ThreadStart()

Any help will be welcome.
Many thanks!

Frankie


5 Answers, 1 is accepted

Sort by
0
Petar Marchev
Telerik team
answered on 16 Nov 2012, 09:17 AM
Hi Frankie,

Thank you for the attached code snippet and explanations. I was able to create a new project based on the provided code, but I was unable to get an exception. Please find the project I ended up with enclosed to this post and an image, that is the output the project produces after a button click. See if you get the same behavior on your end.

If you keep experiencing issues with this you can either let me know how to update the project to get the exception or open a new support ticket with a project attached to it, that reproduces the error so that we can investigate. 

Kind regards,
Petar Marchev
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Dev
Top achievements
Rank 1
answered on 16 Nov 2012, 10:12 AM
Hi Peter,

Thanks for your prompt response and your sample project. I can run with your sample project with Bollinger Band as indicator.

After this, I can figure out why the NullReferenceException occurs and I make little change to your project so that you can re-produce it:

First, I changed PlotInfo with properties of Nullable type:
public class PlotInfo
    {
        public DateTime? Date { get; set; }
        public double? Price { get; set; }
    }

Secondly, I put one null data point in the data:
private object GetData()
        {
            return new List<PlotInfo>()
            {
                new PlotInfo { Date = new DateTime(2012, 11, 16), Price = 33,},
                new PlotInfo { Date = new DateTime(2012, 12, 17), Price = 23,},
                new PlotInfo { Date = new DateTime(2013, 1, 18), Price = 47,},
                new PlotInfo { Date = new DateTime(2013, 2, 19), Price = 44,},
                new PlotInfo { Date = new DateTime(2013, 3, 20), Price = 49,},
                new PlotInfo { Date = new DateTime(2013, 4, 21), Price = 51,},
                //new PlotInfo { Date = new DateTime(2013, 5, 22), Price = 43,},
                new PlotInfo { Date = new DateTime(2013, 5, 22), Price = null,},
                new PlotInfo { Date = new DateTime(2013, 6, 23), Price = 38,},
            };
        }

After this change, when I run the project and click the button to draw the Bollinger Band indicator, NullReferenceException occurs.
Is that a bug in indicator for handling Nullable type which has null value?

Many thanks!

Frankie






0
Petar Marchev
Telerik team
answered on 16 Nov 2012, 12:18 PM
Hi Frankie,

I now see what the issue is, thank you for the clarifications. The Bollinger Bands analysis depend on sums and averages of all items (well, n items, see the moving average for instance). If one of the value is null, then it would not be possible to calculate a proper average value. This is why all values must be valid doubles.

I have notified our developers about the ambiguous exception and in the future a more specific exception will be thrown. However, this behavior is unlikely to change. It is better for an exception to be thrown so that the developer can react to the situation and if needed - remove the items with null values (after all, if the value is null, it does not carry too much information).

If you believe we are incorrect about keeping this behavior please explain your current scenario and requirements and the reasons for this. We will consider it and if we decide that this is a valid case, we might update our code to work in a different manner.  

All the best,
Petar Marchev
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Dev
Top achievements
Rank 1
answered on 19 Nov 2012, 02:29 AM
Hi Peter,

Thanks for your explanation. I understand that most of the indicators need valid data points to work with to achieve its functionalities. However, I would suggest that Exception throwing mechanism may not fit many cases in reality. For example, a NULL price data point from some pricing sources may suggests the stock had been suspended from trading on a particular date. In this case, most of the third-party indicators would simply skip that data point and returns the value of previous date.
I would like to suggest if Telerik RadChartView could have this features or options to skip NULL price data in calculating indicators instead of throwing Exceptions, so as to make the ChartView more flexible.
Many thanks!

Frankie


0
Petar Marchev
Telerik team
answered on 19 Nov 2012, 09:24 AM
Hi Frankie,

I do understand that there are numerous situations in which it is a better option to mute an exception out. However I do not agree this to be one of them.
.. returns the value of previous date
This will only lead to incorrectly visualized data. I think you should simply filter your data prior to passing it to the chart.  

Regards,
Petar Marchev
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

Tags
ChartView
Asked by
Dev
Top achievements
Rank 1
Answers by
Petar Marchev
Telerik team
Dev
Top achievements
Rank 1
Share this question
or