0

I'm trying to develop a video conferencing app using Visual C#. Here is a chunk of the Start_Sending_Video_Conference:

    private void Start_Sending_Video_Conference(string remote_IP,int port_number)
    {
        try
        {
            using ( ms = new MemoryStream())
            {
                //ms = new MemoryStream();// Store it in Binary Array as Stream
                pictureBox1.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                byte[] arrImage = ms.GetBuffer();//returns the array of unsigned bytes
                myclient = new TcpClient(remote_IP, port_number);//Connecting with server
                myns = myclient.GetStream();//returns the stream 
                mysw = new BinaryWriter(myns);//encoding
                mysw.Write(arrImage);//send the stream to above address
                ms.Flush();
                mysw.Flush();
                myns.Flush();
                ms.Close();
                mysw.Close();
                myns.Close();
                myclient.Close();
            }
        }

         catch (Exception ex)
        {

            Capturing.Enabled = false;
            MessageBox.Show(ex.Message,"Video Conference Error Message",MessageBoxButtons.OK,MessageBoxIcon.Error);

        }

    }

I get a

NullReferenceException at pictureBox1.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); The method and arguments to the function are all defined. 'ms' has been defined using the 'new' keyword as can be seen.

All the methods have defined references. What am I missing? How should i solve this?

Andrey Korneyev
  • 26,353
  • 15
  • 70
  • 71
  • `NullReferenceException` is a common situation for beginner programmers. The link provided should help you understand the problem. Then use the debugger to find what/where/when you have a variable that is `null`. – Soner Gönül Feb 17 '15 at 08:04
  • Add a breakpoint on ImageBox1.Image.Save. Is ImageBox1.Image not null? Is ms not null? I suspect ImageBox1.Image does not exist. Otherwise it could be because ms doesn't actually stand for anything until you call GetBuffer(); – Captain Kenpachi Feb 17 '15 at 08:08
  • To quote the MSDN: "new MemoryStream()" initializes a new instance of the MemoryStream class with an expandable capacity initialized to zero(null). – Paul Weiland Feb 17 '15 at 08:15
  • private void WebCamCapture_ImageCaptured(object source, WebCam_Capture.WebcamEventArgs e) { this.pictureBox1.Image = e.WebCamImage; } – Mujtaba Gul Feb 17 '15 at 08:39
  • I have defined this after the Start Sending function. This should fill up pictureBox1.Image but it doesn't @Juann Strauss – Mujtaba Gul Feb 17 '15 at 08:41

0 Answers0