title: nullreferenceexception was unhandled - c#
body: <p>I have downloaded this <a href="http://www.codeproject.com/Articles/21143/PictureBox-Array-in-Visual-Studio-NET" rel="nofollow">PictureBox ImageArray</a><br>
  then I've added a button which will add all it's image to sql server 2005  </p>
  
  <p>this is the code:  </p>
  
  <pre><code>private System.Windows.Forms.PictureBox[] imgArray2;
  string c_string = "server=.\\sqlexpress;database=Blue;trusted_connection=true";
  
  
  for (int i = 0; i &lt; NumOfFiles; i++)
  {
      imgArray2[i].Image = Image.FromFile(imgName[i]);
      string name = imgName[i].Substring(imgName[i].LastIndexOf(@"\") + 1, imgName[i].Length - imgName[i].LastIndexOf(@"\") - 1);
      MemoryStream mstr = new MemoryStream();
      imgArray2[i].Image.Save(mstr, imgArray2[i].Image.RawFormat);
      byte[] arrImage = mstr.GetBuffer();
      string cmd = "insert into Images (ImagePart" + i + "Name, ImagePart" + i + ") values (@PName, @Pic)";
  
      SqlConnection c = new SqlConnection(c_string);
      SqlCommand comm = new SqlCommand(cmd, c);
      comm.Parameters.Add(new SqlParameter("@PName", SqlDbType.VarChar, 40)).Value = name;
      comm.Parameters.Add(new SqlParameter("@Pic", SqlDbType.Image)).Value = arrImage;
  
      try
      {
          c.Open();
          comm.ExecuteNonQuery();
      }
      catch (System.Data.SqlClient.SqlException err)
      {
          MessageBox.Show(err.Message);
      }
      finally
      {
          c.Close();
      }
  
  }
  </code></pre>
  
  <p>I've added an <code>imgArray2[]</code> so the image from <code>imgArray[]</code> won't changes and even though I'm using <code>imgArray[]</code> it still throwing the Null error.</p>
  
  <p>The error info highlights the <code>imgArray2[i].Image = Image.FromFile(imgName[i]);</code><br>
  it shows nullreferenceexception was unhandled - Object reference not set to an instance of an object.</p>