0

I am developing a project in c#. I have a stop button which calls grabber.dispose but when I click it the application crashes and I get the following error:

object reference is not set to an instance of object.

FrameGrabber function should stop the program and move it to currentframe.

Capture grabber:

     private void FDButton_Click(object sender, EventArgs e)
            {
                CameraCapture();
                Application.Idle += new EventHandler(FrameGrabber);
                //initalize Frame grabber event
                FDButton.Enabled = false;
            }

            private void FrameGrabber(object sender, EventArgs e)
            {


                    currentFrame = grabber.QueryFrame().Resize(320, 240, Emgu.CV.CvEnum.INTER.CV_INTER_CUBIC);
                    //convert frame to gray scale
                    gray = currentFrame.Convert<Gray, Byte>();
                    //now detect face by using classifier
                    MCvAvgComp[][] facesDetected = gray.DetectHaarCascade(

                        face,//name of cascade
                        1.2,
                        10,
                        Emgu.CV.CvEnum.HAAR_DETECTION_TYPE.DO_CANNY_PRUNING,
                        new Size(20, 20));

                    //now check each frame of imagebox containing video and detect face
                    foreach (MCvAvgComp f in facesDetected[0])
                    {
                        //if face found increment t
                        t = t + 1;
                        //now see result by copying detected face in a frame name as result
                        result = currentFrame.Copy(f.rect).Convert<Gray, byte>().Resize(100, 100, Emgu.CV.CvEnum.INTER.CV_INTER_CUBIC);
                        //we convert current frame to gray scale and resize to 200x100
                        //now draw rectangle on detected image
                        currentFrame.Draw(f.rect, new Bgr(Color.Red), 2);
                    }



                    //view currentframe in imported imagebox
                    DetectionImageBox.Image = currentFrame;



            }

Camera Capture Function:

 public void CameraCapture()
        {
            grabber = new Capture();
            grabber.QueryFrame();
        }

Cancel Button:

        private void FDStopButton_Click(object sender, EventArgs e)
        {
            grabber.Dispose();

        } 
Munib
  • 1
  • 2
  • Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – TheGeneral Apr 29 '18 at 08:46
  • thats no tmy problem. – Munib Apr 29 '18 at 08:51

1 Answers1

0

This is most likely occurring because you've subscribed to the Idle event handler.

Application.Idle += new EventHandler(FrameGrabber);

FrameGrabber is still subscribed, but by calling dispose, you've set FrameGrabber to a null reference, so when the event triggers, you get an exception.

You'll want to unsubcribe from the event handler so this doesn't occur:

   private void FDStopButton_Click(object sender, EventArgs e)
   {
       Application.Idle -= FrameGrabber;
       grabber.Dispose();
   } 

You can look here for a better description of unsubscribing: https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/events/how-to-subscribe-to-and-unsubscribe-from-events

Jace
  • 21
  • 6