Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
未处理nullreferenceexception-c#_C# - Fatal编程技术网

未处理nullreferenceexception-c#

未处理nullreferenceexception-c#,c#,C#,我已经下载了这个 然后我添加了一个按钮,该按钮将把它的所有映像添加到SQLServer2005 代码如下: private System.Windows.Forms.PictureBox[] imgArray2; string c_string = "server=.\\sqlexpress;database=Blue;trusted_connection=true"; for (int i = 0; i < NumOfFiles; i++) { imgArray2[i].Im

我已经下载了这个
然后我添加了一个按钮,该按钮将把它的所有映像添加到SQLServer2005

代码如下:

private System.Windows.Forms.PictureBox[] imgArray2;
string c_string = "server=.\\sqlexpress;database=Blue;trusted_connection=true";


for (int i = 0; i < 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();
    }

}
private System.Windows.Forms.PictureBox[]imgArray2;
string c_string=“server=。\\sqlexpress;database=Blue;trusted_connection=true”;
对于(int i=0;i
我添加了一个
imgArray2[]
,因此来自
imgArray[]
的图像不会改变,即使我使用
imgArray[]
它仍然抛出空错误

错误信息突出显示了
imgArray2[i].Image=Image.FromFile(imgName[i])

它显示未处理nullreferenceexception-对象引用未设置为对象的实例。

在使用它之前,您需要实例化
System.Windows.Forms.PictureBox[]imgArray2
数组。

您需要实例化
System.Windows.Forms.PictureBox[]imgArray2
数组,然后再使用它。

imgArray2
值及其内容从未初始化。它需要是一个能够容纳至少
NumFiles
项的数组,并且每个项都需要初始化

imgArray2 = new System.Windows.Forms.PictureBox[NumFiles];
for (int i = 0; i < NumOfFiles; i++)
  imgArray2[i] = new System.Windows.Forms.PictureBox();
imgArray2=newsystem.Windows.Forms.PictureBox[NumFiles];
对于(int i=0;i
imgaray
值及其内容从未初始化。它需要是一个能够容纳至少
NumFiles
项的数组,并且每个项都需要初始化

imgArray2 = new System.Windows.Forms.PictureBox[NumFiles];
for (int i = 0; i < NumOfFiles; i++)
  imgArray2[i] = new System.Windows.Forms.PictureBox();
imgArray2=newsystem.Windows.Forms.PictureBox[NumFiles];
对于(int i=0;i
我只是简单地看了一下,你的
imgArray2
似乎从未被实例化过。。。您刚刚声明了一个变量并试图访问它

我只是简单地看了一下,看起来您的
imgArray2
从未被实例化过。。。您刚刚声明了一个变量并试图访问它

这种问题可以通过使用调试器跟踪空引用发生的位置来解决。请不要让其他人为您调试代码。这种问题可以通过使用调试器跟踪空引用发生的位置来解决。请不要让其他人为您调试代码之后将完成该工作,此
imgArray2[i]=new System.Windows.Forms.PictureBox()之后将完成工作