【问题标题】:nullreferenceexception was unhandled on filling up array - c#填充数组时未处理 nullreferenceexception - c#
【发布时间】:2012-02-28 16:36:06
【问题描述】:

我正在尝试检索保存在 sql 数据库中的所有名称
然后突然我得到了

nullreferenceexception 在填充数组时未处理 - 对象 引用未设置为对象的实例。

这是我的代码:

我收到了错误

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

在那部分,我想用 imgName 数组填充所有的名字

我该如何解决?请帮忙

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"]);

    }
}​

【问题讨论】:

  • 等等!为什么在关闭连接后调用updatedata?不计算。

标签: c#


【解决方案1】:

您需要实例化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"]);

        } 
    }
}

【讨论】:

    【解决方案2】:

    那是因为你没有指定数组的大小...要么指定数组的大小,要么使用arraylist / generic list

    试试这个: 包括命名空间: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"]));
            } 
        }
    

    【讨论】:

      【解决方案3】:

      你还没有创建数组,你只是创建了可以保存对数组的引用的变量。

      知道有多少行后创建数组:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-04-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多