Programmatically Creating DrawLineItem Error Null Handles Exception

TeeChart for Microsoft Visual Studio .NET, Xamarin Studio (Android, iOS & Forms) & Monodevelop.
Post Reply
jzarech
Newbie
Newbie
Posts: 58
Joined: Mon Jul 07, 2008 12:00 am

Programmatically Creating DrawLineItem Error Null Handles Exception

Post by jzarech » Mon Nov 09, 2020 7:52 am

I'm adding DrawLineItems to the Lines collection and that's fine --

Until the chart tries to refresh and throws an exception because all the "Handles" fields such as EndHandle, MidlleBottomHandle, MiddleTopHandle, and Starthandle are Null so a NullReferenceException is thrown.

My question is how to either populate the Handle values or avoid the exception when programmatically adding DrawLineItems to a .Net TeeChart?

Error
Error.gif
Error.gif (52.58 KiB) Viewed 11976 times
My populate code:
howtopopulate.gif
howtopopulate.gif (51.04 KiB) Viewed 11976 times
Null "Handle's" in DrawLineItems
nullDrawLineItems.gif
nullDrawLineItems.gif (48.27 KiB) Viewed 11976 times
Also when I add the line to the DrawLines Lines collection it doesn't throw the newline event like when I draw the line on the screen by hand with the mouse.

Thanks for your help.

Joseph

Christopher
Guru
Posts: 1603
Joined: Fri Nov 15, 2002 12:00 am

Re: Programmatically Creating DrawLineItem Error Null Handles Exception

Post by Christopher » Mon Nov 09, 2020 10:05 am

Hello Joseph,

I think the reason is that you are using the parameterless constructor of DrawLineItem, which means that EndHandle will throw an error (following code is from the sourcecode):

Code: Select all

        public Rectangle EndHandle
        {
            get
            {
                return RectangleFromPoint(tool.AxisPoint(EndPos));
            }
        }
It will throw an error because the tool variable is null - this is the parameter you can pass in the other constructor of DrawLineItem, e.g.

Code: Select all

        private void InitializeChart(TChart chart)
        {
            chart.Series.Add(typeof(Line)).FillSampleValues();

            var drawLine = new DrawLine(chart.Chart);

            var item = new DrawLineItem(drawLine); //<-- pass in the 'tool' here
            item.Pen.Width = 3;
            item.Pen.Color = Color.Red;
            item.StartPos = new PointDouble(1, 1);
            item.EndPos = new PointDouble(3, chart.Series[0].YValues.Maximum);

            drawLine.Lines.Add(item);
        }
Best Regards,
Christopher Ireland / Development & Support
Steema Software
Avinguda Montilivi 33, 17003 Girona, Catalonia
Tel: 34 972 218 797
http://www.steema.com
Instructions - How to post in this forum

jzarech
Newbie
Newbie
Posts: 58
Joined: Mon Jul 07, 2008 12:00 am

Re: Programmatically Creating DrawLineItem Error Null Handles Exception

Post by jzarech » Mon Nov 09, 2020 6:16 pm

Christopher - u r correct! I feel dumb for missing the constructor that takes the drawline object as a param.
fixed.gif
fixed.gif (60.87 KiB) Viewed 11963 times
Thanks soooooo much!!!

Christopher
Guru
Posts: 1603
Joined: Fri Nov 15, 2002 12:00 am

Re: Programmatically Creating DrawLineItem Error Null Handles Exception

Post by Christopher » Tue Nov 10, 2020 8:51 am

Hello Joseph!
jzarech wrote:
Mon Nov 09, 2020 6:16 pm
Thanks soooooo much!!!
You are very welcome! Don't forget that debugging such issues is considerably easier with the source code :)
Best Regards,
Christopher Ireland / Development & Support
Steema Software
Avinguda Montilivi 33, 17003 Girona, Catalonia
Tel: 34 972 218 797
http://www.steema.com
Instructions - How to post in this forum

Post Reply