Nel

Nel

  • NA
  • 716
  • 1.1m

Object reference not set to an instance of an object.

Feb 22 2012 4:24 AM
Hi,

I have a picturebox in one form. When inserting data, I am filling the picturebox as well, together with other types objects, and on clikc on the picturebox I can open another window aith a whole picture of the document.
But when the data is inserted in the database, and I click on one row of the datagridview, all the objects are filled, and I want again when clicking on the picturebox to open the picture in a new window, but I get error.
"Object reference not set to an instance of an object." 
in the bold row below.

The code for the form where I open the whole picture is:

 public partial class Slik : Form
    {
        public Slik()
        {
            InitializeComponent();
            Dogovor dog = (Dogovor)Application.OpenForms["Dogovor"];
            DataGridView dgv = (DataGridView)dog.Controls["dataGridView2"];
                      
                byte[] data = (byte[])dgv.SelectedRows[0].Cells[3].Value;
                MemoryStream ms = new MemoryStream(data);
                pictureBox1.Image = Image.FromStream(ms);
           
        }

        private void Slik_Load(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Maximized;
        }     

       
    }



and the code for the first form which calls this second form is:

private void pictureBox2_Click(object sender, EventArgs e)
        {
            if (pateka != "")
            {
                Slika slika = new Slika(izbranfajl);
                slika.Show();
            }
            else
            {
               
                Slik slika1 = new Slik();
                slika1.Show();
              
            }
        }

Can anybody help me please?Thank you in advance.

Answers (6)