System.NullReferenceException thrown in the MouseMove

A forum dedicated to WinForms version of LightningChart Ultimate.

Moderator: Queue Moderators

Post Reply
Kountree
Posts: 21
Joined: Mon Jan 26, 2015 10:36 pm

System.NullReferenceException thrown in the MouseMove

Post by Kountree » Wed Oct 28, 2015 7:54 pm

To whomever it may concern:

As of the past two weeks, users of the arction tool are receiving an error

************** Exception Text **************
System.NullReferenceException: Object reference not set to an instance of an object.
at Arction.LightningChartUltimate.Views.ViewXY.ViewXY.MouseMoved(MouseEventArgs eventArguments, MouseEventType eventType, LightningChartUltimate chart)
at Arction.LightningChartUltimate.Views.ViewBase.KQB(MouseEventArgs A, MouseEventType B, LightningChartUltimate C)
at Arction.LightningChartUltimate.LightningChartUltimate.OnMouseMove(MouseEventArgs eventArgs)
at System.Windows.Forms.Control.WmMouseMove(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.ContainerControl.WndProc(Message& m)
at System.Windows.Forms.UserControl.WndProc(Message& m)
at Arction.LightningChartUltimate.LightningChartUltimate.WndProc(Message& message)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

Here is the picture.
This is the error message that appears at random occurances
This is the error message that appears at random occurances
Capture.PNG (223.15 KiB) Viewed 12288 times
Usually, I would provide a set of steps in order to repeat the error. Unfortunately, this exception is thrown randomly.

Thank you
Kountree

User avatar
ArctionPasi
Posts: 1367
Joined: Tue Mar 26, 2013 10:57 pm
Location: Finland
Contact:

Re: System.NullReferenceException thrown in the MouseMove

Post by ArctionPasi » Wed Oct 28, 2015 9:08 pm

These kinds of errors are typically caused by forgetting to use Control.Invoke when updating the chart from background thread. Can you check you are using Invoke correctly?
LightningChart Support Team, PT

Kountree
Posts: 21
Joined: Mon Jan 26, 2015 10:36 pm

Re: System.NullReferenceException thrown in the MouseMove

Post by Kountree » Wed Oct 28, 2015 9:35 pm

Thanks for the response. The Invoke member takes a delegate as a parameter. Is there an example on how to do this? And the biggest question, is how to recreate this error

User avatar
ArctionPasi
Posts: 1367
Joined: Tue Mar 26, 2013 10:57 pm
Location: Finland
Contact:

Re: System.NullReferenceException thrown in the MouseMove

Post by ArctionPasi » Wed Oct 28, 2015 10:12 pm

In WinForms demo application, there's
- ViewXY->RealTime-> Thread-fed multi-channel example
- ViewXY->RealTime-> Historic data review example

Invoke is used there correctly.


Or just use this simpler example. It's using a Task as background thread.

Code: Select all

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading; 
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Arction.WPF.LightningChartUltimate; 
using Arction.WPF.LightningChartUltimate.SeriesXY;
using Arction.WPF.LightningChartUltimate.Axes;
using Arction.WPF.LightningChartUltimate.Views;
using Arction.WPF.LightningChartUltimate.Views.ViewXY; 

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        
        System.Threading.CancellationTokenSource m_cancel = new CancellationTokenSource();
        LightningChartUltimate m_chart;
        int ChannelCount = 10;
        Random m_rand = new Random(); 

        public MainWindow()
        {
            InitializeComponent();

            CreateChart();

            Task task = new Task(() => { ThreadProc(); }, m_cancel.Token);
            task.Start();

            this.Closing += new System.ComponentModel.CancelEventHandler(MainWindow_Closing);
        }

        void MainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            m_cancel.Cancel(); 
        }


        void CreateChart()
        {
            m_chart = new LightningChartUltimate();


            m_chart.BeginUpdate();
            ViewXY v = m_chart.ViewXY;

            AxisX xAxis = v.XAxes[0];
            xAxis.SetRange(0, 10);
                        
            v.YAxes.Clear(); 

            for (int iCh = 0; iCh < ChannelCount; iCh++)
            {
                AxisY yAxis = new AxisY(v);
                yAxis.AxisColor = DefaultColors.SeriesForBlackBackgroundWPF[iCh % DefaultColors.SeriesForBlackBackgroundWPF.Length]; 
                v.YAxes.Add(yAxis);

                SampleDataSeries s = new SampleDataSeries(v, xAxis, yAxis);
                s.SamplingFrequency = 1000;
                s.FirstSampleTimeStamp = 0; 
                s.LineStyle.Color = DefaultColors.SeriesForBlackBackgroundWPF[iCh % DefaultColors.SeriesForBlackBackgroundWPF.Length];
                s.LineStyle.Width = 1; 
                s.MouseInteraction = false; 
                v.SampleDataSeries.Add(s); 
            }

            v.AxisLayout.YAxesLayout = YAxesLayout.Stacked;
            v.LegendBox.Visible = false; 

            m_chart.EndUpdate();

            gridMain.Children.Add(m_chart); 
        }

        void ThreadProc()
        {
            while (!m_cancel.IsCancellationRequested)
            {
                int iSampleCount = 10*1000; //X axis lenght * sampling frequency 
                List<double[]> dataForAllChannels  = new List<double[]>(); 
                for (int iCh = 0; iCh < ChannelCount; iCh++)
                { 
                    
                    double[] data = new double[iSampleCount]; 
                    for(int i=0; i<iSampleCount;i ++)
                    {
                        data[i] = m_rand.NextDouble()* 10.0; 
                    }

                    dataForAllChannels.Add(data); 
                }

                
                Dispatcher.Invoke((Action)delegate { UpdateChart(dataForAllChannels); });
                
                Thread.Sleep(1); 

            }

            if (m_chart != null)
            {
                m_chart.Dispose();
                m_chart = null;
            }
        }

        void UpdateChart(List<double[]> dataForAllChannels)
        {
            m_chart.BeginUpdate();

            int iChCount = dataForAllChannels.Count;

            for (int iCh = 0; iCh < iChCount; iCh++)
            {
                m_chart.ViewXY.SampleDataSeries[iCh].SamplesDouble = dataForAllChannels[iCh]; 
            } 

            m_chart.EndUpdate(); 

        }

    }
}

There's Dispatcher.Invoke((Action)delegate { UpdateChart(dataForAllChannels); });

For WinForms, just replace Dispatcher with 'this', and it uses the Form:
this.Invoke((Action)delegate { UpdateChart(dataForAllChannels); });
LightningChart Support Team, PT

Kountree
Posts: 21
Joined: Mon Jan 26, 2015 10:36 pm

Re: System.NullReferenceException thrown in the MouseMove

Post by Kountree » Thu Oct 29, 2015 12:36 pm

Dood,

When it comes to tech support, you are making the competition look really horrible.

Thank you for the example. Those were the examples that I used, but I did not call the invoke, but instead the method from the thread
this.UIThread(delegate
{
// populate my array
this.Invoke((Action)delegate { FeedNewDataToChart(aMultiChannelData); });
}

I added the Invoke which is already nested in my UIThread.

I am not quit sure that this is a fix. Reason being is that I cannot reproduce the error.

Kountree
Posts: 21
Joined: Mon Jan 26, 2015 10:36 pm

Re: System.NullReferenceException thrown in the MouseMove

Post by Kountree » Thu Oct 29, 2015 3:36 pm

Hi ArctionPasi,

After careful analysis of my code, I do infact call the Control.Invoke. In fact, the examples you provided, I used them in my code.

It appears to happen when I set my plot to sweep. In the debugger, I get the mousemove exception even when I do not move the mouse.

Thanks
Kountree

User avatar
ArctionPasi
Posts: 1367
Joined: Tue Mar 26, 2013 10:57 pm
Location: Finland
Contact:

Re: System.NullReferenceException thrown in the MouseMove

Post by ArctionPasi » Fri Oct 30, 2015 2:51 pm

Are you setting the XAxis.ScrollMode = Sweeping in background thread?

Please send us a small VS project to reproduce the issue.
LightningChart Support Team, PT

Post Reply