object reference not set to an instance of Object

firstoption's Avatar, Join Date: Apr 2014
Light Poster
Good day to all,
Please I need somebody to help me have a look at my codes.I am having this error of Object reference not set to an instance Of Object.It appears the error is within this lines of codes
Code:
 if (_scrollingTimer  == null)
            {
                _scrollingTimer = new Timer()
                {
                    Enabled = false,
                    Interval = 500,
                    Tag = (sender as TrackBar).Value
                };
but unfortunately I was unable to resolve this error.I would be very glad if somebody could help me out.thank you for the usual support.best regards.
Firstoption.
Below are the remaining part of the codes.

Code:
 byte[] data = new byte[5];
 private Timer _scrollingTimer = null;

  private void button3_Click(object sender, EventArgs e)
        {

            UInt32 numBytesWritten = 0;
            data[0] = 1;
            myFtdiDevice.Write(data, 1, ref numBytesWritten);
            data[0] = 0x6A;
            myFtdiDevice.Write(data, 1, ref numBytesWritten);


        }
       
  private void trackBar1_Scroll(object sender, EventArgs e)
        {

           if(!backgroundWorker1.IsBusy)
           {

               backgroundWorker1.RunWorkerAsync();
           }
        
        }
     private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            UInt32 numBytesWritten = 1;
            string dataToWrite = "#0";

            if (_scrollingTimer  == null)
            {
                _scrollingTimer = new Timer()
                {
                    Enabled = false,
                    Interval = 500,
                    Tag = (sender as TrackBar).Value
                };
                _scrollingTimer.Tick += (s, ea) =>
                {
                    if (trackBar1.Value == (int)_scrollingTimer.Tag)
                    {
                        _scrollingTimer.Stop();
                        myFtdiDevice.Write(dataToWrite, dataToWrite.Length, ref numBytesWritten);
                        int percent = (int)(((double)trackBar1.Value / (double)trackBar1.Maximum) * 100);
                        label2.Text = (percent.ToString()) + "%";
                        data[0] = Convert.ToByte(percent);
                        data[1] = 0x6A;
                        myFtdiDevice.Write(data, 2, ref numBytesWritten);
                        _scrollingTimer.Dispose();
                        _scrollingTimer = null;
                    }
                    else
                    {
                        _scrollingTimer.Tag = trackBar1.Value;
                    }
                };
                _scrollingTimer.Start();
            }
        }
0
kush_2207's Avatar, Join Date: Jun 2007
Go4Expert Member
This line Tag = (sender as TrackBar).Value
is causing the NullReferenceException (sender is BackgroundWorker here even though you are calling RunWorkerAsync from TrackBar's scroll event. It is the BackgroundWorker who raises the DoWork event)

Change it to
Tag = trackBar1.Value