1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

object reference not set to an instance of Object

Discussion in 'C#' started by firstoption, May 16, 2014.

  1. firstoption

    firstoption New Member

    Joined:
    Apr 1, 2014
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    0
    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();
                }
            }
       
    
    
    
    
     
  2. kush_2207

    kush_2207 New Member

    Joined:
    Jun 26, 2007
    Messages:
    49
    Likes Received:
    1
    Trophy Points:
    0
    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
     

Share This Page