Click here to Skip to main content
15,911,139 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
// I want to capture image from USB camera but my application show error:
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using OpenCVDotNet;

namespace WindowsApplication6
{
    public partial class Form1 : Form
    {
        private CVCapture capture;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            capture = new CVCapture(0);
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
                using (CVImage nextFrame = capture.QueryFrame())
                {
                    selectPictureBox1.Image = nextFrame.ToBitmap(); // THIS LINE ERROR
	            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            timer1.Enabled = !(timer1.Enabled);
        }
    }
}

Error show : "Object reference not set to an instance of an object"

Please help me fix this error. Thanks !
Posted
Updated 1-Nov-11 22:20pm
v2

Either selectPictureBox1 or nextFrame, or both, is/are not initialized at the moment the marked line is executed.

Maybe QueryFrame() failed and left nextFrame set to null?
 
Share this answer
 
Comments
hien123 2-Nov-11 5:32am    
Thanh you lukeer !

QueryFrame() failed.That why nextFrame == null.

my problem is how nextFrame!=null.

please help me again
lukeer 2-Nov-11 6:40am    
I'm sorry. I have no idea of how CVCapture.QueryFrame() should work. I guess you'll have to read the documentation.
the error message is very generic..
check null condition for all your objects before use.
like, it will give you an idea that which object is null.

C#
if(nextFrame != null)
{
   if(selectPictureBox1 != null)
   { 
      selectPictureBox1.Image = nextFrame.ToBitmap(); 
   }
}


if selectPictureBox1 object is null, check for that. if nextFrame object is null check for the solution regarding that, means your 'using (CVImage nextFrame = capture.QueryFrame())' is not working.
-> check camera connection, if possible click photos before using with your app for varification.
-> check for help in CVCapture API
 
Share this answer
 
Comments
hien123 2-Nov-11 5:35am    
your right but i can not capture image because nextFrame == null.
How to nextFrame !=null ?
please help me!
hien123 2-Nov-11 5:56am    
when my application run to this line: capture = new CVCapture(0);
i see signal lights on camera. Its mean connected ????

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900