C Sharp : error Object Reference not set to an instance of an Object

Asked

Viewed 575 times

-1

Guys, I’m a beginner in C Sharp, and using VS community, so I found a web cam program and adapted the code to where I want to use here, but when I click save image, it appears this error:

error Object Reference not set to an instance of an Object

And ends up not saving, someone can help me? I’ve changed the way you save but nothing has changed.

Follows the code:

    using System;
    using System.Drawing;
    using System.Windows.Forms;
    using System.Drawing.Imaging;

    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            public DirectX.Capture.Filter Camera;
            public DirectX.Capture.Capture CaptureInfo;
            public DirectX.Capture.Filters CamContainer;
            Image capturaImagem;
            public string caminhoImagemSalva = null;

            public Form1()
            {
                InitializeComponent();
            }

            private void Form1_Load(object sender, EventArgs e)
            {
               CamContainer = new DirectX.Capture.Filters();  
               try
               {
                 int no_of_cam = CamContainer.VideoInputDevices.Count;

                 for (int i = 0; i < no_of_cam; i++ )
                 {
                    try
                    {
                            // Obtém o dispositivo de entrada de video
                            Camera = CamContainer.VideoInputDevices[i];

                            // inicializa e captura o dispostivo
                            CaptureInfo = new DirectX.Capture.Capture(Camera, null);

                            // Define a janela de visualização
                            CaptureInfo.PreviewWindow = this.picWebCam;

                            // Capturando o tratamento do evento
                            CaptureInfo.FrameCaptureComplete += AtualizaImagem;

                            // Captura o frame do dispositivo
                            CaptureInfo.CaptureFrame();

                            // Se o dispositivo foi encontrado sem checar o resto
                            break;
                    }
                    catch (Exception ex) 
                    {
                        throw ex;    
                    }
                }
            }
            catch (Exception ex)
            {                
                MessageBox.Show(this, ex.Message);
            }
         }

            public void AtualizaImagem(PictureBox frame)
            {
                try
                {
                    capturaImagem = frame.Image;
                    this.picImagem.Image = capturaImagem;
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Erro " + ex.Message);
                }
            }

            private void btnCaptura_Click(object sender, EventArgs e)
            {
                try
                {
                    CaptureInfo.CaptureFrame();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Erro " + ex.Message);
                }
            }

            private void btnSalvar_Click(object sender, EventArgs e)
            {
                try
                {
                    caminhoImagemSalva = @"c:\dados\" + "ImagemWebCam" + DateTime.Now.Day.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Year.ToString() + DateTime.Now.Millisecond.ToString() + ".jpg";
                    picImagem.Image.Save(caminhoImagemSalva, ImageFormat.Jpeg);
                    MessageBox.Show("Imagem salva com sucesso");
                }
                catch(Exception ex)
                {
                    MessageBox.Show("Erro " + ex.Message);
                }
            }

            private void picWebCam_Click(object sender, EventArgs e)
            {

            }

            private void pictureBox1_Click(object sender, EventArgs e)
            {

            }
        }

}

breakpoint:

<?xml version="1.0" encoding="UTF-8"?>

-<BreakpointCollection xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">


-<Breakpoints>


-<Breakpoint>

<Version>15</Version>

<IsEnabled>1</IsEnabled>

<IsVisible>1</IsVisible>

<IsEmulated>0</IsEmulated>

<IsCondition>0</IsCondition>

<ConditionType>WhenTrue</ConditionType>

<LocationType>SourceLocation</LocationType>


-<TextPosition>

<Version>4</Version>

<FileName>.\CapturaImagemWebCam\Form1.cs</FileName>

<startLine>93</startLine>

<StartColumn>16</StartColumn>

<EndLine>93</EndLine>

<EndColumn>75</EndColumn>

<MarkerId>0</MarkerId>

<IsLineBased>0</IsLineBased>

<IsDocumentPathNotFound>0</IsDocumentPathNotFound>

<ShouldUpdateTextSpan>1</ShouldUpdateTextSpan>


-<Checksum>

<Version>1</Version>

<Algorithm>00000000-0000-0000-0000-000000000000</Algorithm>

<ByteCount>0</ByteCount>

<Bytes/>

</Checksum>

</TextPosition>

<NamedLocationText>WindowsFormsApplication1.Form1.btnSalvar_Click(object sender, EventArgs e)</NamedLocationText>

<NamedLocationLine>6</NamedLocationLine>

<NamedLocationColumn>0</NamedLocationColumn>

<HitCountType>NoHitCount</HitCountType>

<HitCountTarget>1</HitCountTarget>

<Language>3f5162f8-07c6-11d3-9053-00c04fa302a1</Language>

<IsMapped>0</IsMapped>

<BreakpointType>PendingBreakpoint</BreakpointType>


-<AddressLocation>

<Version>0</Version>

<MarkerId>0</MarkerId>

<FunctionLine>0</FunctionLine>

<FunctionColumn>0</FunctionColumn>

<Language>00000000-0000-0000-0000-000000000000</Language>

</AddressLocation>

<DataCount>4</DataCount>

<IsTracepointActive>0</IsTracepointActive>

<IsBreakWhenHit>1</IsBreakWhenHit>

<IsRunMacroWhenHit>0</IsRunMacroWhenHit>

<UseChecksum>1</UseChecksum>

<Labels/>

<RequestRemapped>0</RequestRemapped>

<parentIndex>-1</parentIndex>

</Breakpoint>

</Breakpoints>

</BreakpointCollection>
  • Have to put the stacktrace of the problem?

  • I couldn’t test the code, but does a test by changing the path to my documents or desktop.

  • Jefferson doesn’t know much about how stacktrace works, can help me where I enter the code?

1 answer

1

This message means that you have tried to access a property of a null object. From the code you pasted here, it appears to be the "picImage" variable that hasn’t been started. I couldn’t find where it was declared, you must have left to paste a part of the code here... But you can imagine that the problem is the method "Updatefire" has not been called before the method "btnSalvar_Click", because the method "Updatefire" fills the value of "picImage".

  • I put a breakpoint and gave me the following result , can not post everything here I will put the own question beauty?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.