简体   繁体   English

在填充数组时未处理nullreferenceexception-C#

[英]nullreferenceexception was unhandled on filling up array - c#

I am trying to retrieve all names that was saved at the sql database 我正在尝试检索保存在sql数据库中的所有名称
then suddenly I got the 然后突然我得到了

nullreferenceexception was unhandled on filling up array - Object reference not set to an instance of an object. 在填充数组时未处理nullreferenceexception-未将对象引用设置为对象的实例。

this was my code: 这是我的代码:

I got the error at the 我在错误

  imgName[i] = Convert.ToString(dt.Rows[i]["FinalImageName"]);  

in that part, I'd like to fill imgName array all the name 在那部分,我想用所有名称填充imgName数组

how could i fix it?, help please 我该如何解决?,请帮忙

string c_string = "server=.\\sqlexpress;database=Blue;trusted_connection=true";

public static string ImageToShow;
private int NumOfFiles;
private string[] imgName;

SqlConnection c = new SqlConnection(c_string);
//SqlCommand cm = new SqlCommand(cmd,c);

private void frmMain_Load(object sender, EventArgs e) {
    SqlConnection c = new SqlConnection(c_string);

    try {
        c.Open();
    }
    catch (SqlException ee) {
        MessageBox.Show(ee.Message);
    }
    finally {
        c.Close();
    }
    updateData();
}

private void updateData() {

    SqlConnection c = new SqlConnection(c_string);

    SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM FinalImages", c);

    DataTable dt = new DataTable();
    da.Fill(dt);

    NumOfFiles = dt.Rows.Count;
    for (int i = 0; i < dt.Rows.Count; i++) {
        imgName[i] = Convert.ToString(dt.Rows[i]["FinalImageName"]);

    }
}​

You need to instantiate the imgName array . 您需要实例化imgName数组。

private void updateData()
{
    {

        SqlConnection c = new SqlConnection(c_string);

        SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM FinalImages", c);

        DataTable dt = new DataTable();
        da.Fill(dt);

        NumOfFiles = dt.Rows.Count;
        imgName = new string[NumOfFiles];
        for (int i = 0; i < NumOfFiles; i++)
        {
            imgName[i] = Convert.ToString(dt.Rows[i]["FinalImageName"]);

        } 
    }
}

That is because you have not specified the size of array... either specify size of array or use arraylist / generic list 那是因为你没有指定数组的大小...要么指定数组的大小,要么使用arraylist / generic list

Try this: include namespace : using System.Collections.Generic; 试试这个:include namespace: using System.Collections.Generic;

string c_string = "server=.\\sqlexpress;database=Blue;trusted_connection=true";

    public static string ImageToShow; 
    private int NumOfFiles;
    //private string[] imgName;
   List<string> imgName= new List<string>();

SqlConnection c = new SqlConnection(c_string);
        //SqlCommand cm = new SqlCommand(cmd,c);


   private void frmMain_Load(object sender, EventArgs e)
    {
        SqlConnection c = new SqlConnection(c_string);

        try
        {
            c.Open();
        }
        catch (SqlException ee)
        {
            MessageBox.Show(ee.Message);
        }
        finally
        {
            c.Close();
        }
        updateData();
    }

private void updateData()
    {

        SqlConnection c = new SqlConnection(c_string);

        SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM FinalImages", c);

        DataTable dt = new DataTable();
        da.Fill(dt);

        NumOfFiles = dt.Rows.Count;
        for (int i = 0; i < dt.Rows.Count; i++)
        {
           // imgName[i] = Convert.ToString(dt.Rows[i]["FinalImageName"]);
           imgName.Add(Convert.ToString(dt.Rows[i]["FinalImageName"]));
        } 
    }

You haven't created an array, you have only created the variable that can hold the reference to the array. 您尚未创建数组,仅创建了可以保存对数组的引用的变量。

Create the array once you know how many rows there are: 一旦知道有多少行,就创建数组:

imgName = new string[dt.Rows.Count];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM