【问题标题】:Object reference not set to an instance of an object for combo box对象引用未设置为组合框的对象实例
【发布时间】:2011-10-16 06:33:29
【问题描述】:

我看到许多对象引用未设置为对象的实例,但我在任何问题中都找不到我的场景。

我有一个名为comboBox1 的组合框。在表单加载时,我有代码来填充组合框:

  private void Form1_Load(object sender, EventArgs e)
  {
        // TODO: This line of code loads data into the
        // 'tenderDBDataSet.tbl_Tender_To_Details' table.
        // You can move, or remove it, as needed.
        OleDbCommand cmd = new OleDbCommand("SELECT DISTINCT 
            tbl_Tender_To_Details.To_Name, tbl_Tender_To_Details.To_Address1, 
            tbl_Tender_To_Details.To_Address2, 
            tbl_Tender_To_Details.To_City, tbl_Tender_To_Details.To_PinCode "+
            "FROM tbl_Tender_To_Details "+
            "WHERE to_Name IS NOT NULL ", conn);
        try
        {
            conn.Open();
            OleDbDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                comboBox1.Items.Add(reader["To_Name"]);
                // listBox1.Items.Add(reader[0].ToString());
                // MessageBox.Show(reader[0].ToString());
            }
            reader.Close();
            comboBox1.SelectedIndex = 0;
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        finally
        {
            conn.Close();
        }
    }

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        MessageBox.Show(comboBox1.SelectedValue.ToString());
    }

MessageBox.Show(comboBox1.SelectedValue.ToString()); 行显示:

“对象引用未设置为组合框对象的实例”。

但令我惊讶的是,索引 0 处的值设置为组合框,而表单在此对象引用消息框之后加载。

【问题讨论】:

  • 在while循环后获取combobox item count,它会告诉很多信息

标签: c# winforms combobox visual-studio-2005


【解决方案1】:

您可以尝试在页面加载时将此语句放入并确保组合框已加载项目??

comboBox1.SelectedIndex = 0; 

如果您使用的是 winforms,请尝试将该语句放入 initializecomponent() 函数中

comboBox1.SelectedIndex = 0; 

【讨论】:

  • 您好 errorstacks,感谢您的回复.. 但我已将此语句仅用于表单加载....如何添加页面加载
  • @shanmugamgsn 你也标记了 asp.net,所以我想你正在使用 asp.net 但是,在代码中你提到了组合框 ....
  • 对不起,我将删除 asp.net 标签。我正在使用 Visual Studio 2005 C#。仅限 Windows 应用程序,而非 Web 应用程序
  • @shanmugamgsn 在这一行设置断点 comboBox1.Items.Add(reader["To_Name"]);并在调试模式下尝试并检查是否将项目添加到组合框中...
  • 您好我试过调试。我在 catch 块中发现了错误。 catch (Exception ex) { MessageBox.Show(ex.Message); }
【解决方案2】:

首先,您不要将所选索引显式设置为 0。默认为 0。执行阅读器后可能没有从数据库加载任何内容,因此组合框的 DataSource 为空。在这种情况下,如果您尝试将选定索引设置为 0,则会引发空引用异常,因为框架尝试检索数据源中为空的第一个项目。在这种情况下,您选择的索引应该是 -1。

因此,如果您希望选定的索引成为列表中的第一项,我不会明确设置选定的项目。这是组合框的默认行为。

【讨论】:

  • 嗨 Suhas,但正如你所说,如果我删除 SelectedIndex=0 行,我的 Combobox 会变空.. 但我可以在下面看到这些项目...有什么办法吗?
  • @shanmugamgsn 如果你把 selectindex = -1 组合框填充项目并且在表单加载(即)combobox.Text =0 时不显示此属性中的任何项目
  • 抱歉错误出现在 MessageBox.Show(comboBox1.SelectedValue.ToString());
【解决方案3】:

首先,您是否尝试使用调试器来检查阅读器是否真的在放任何东西?

我注意到您在阅读器中大写了“To_Name”,但在 where 子句中没有 - 您确定它不区分大小写吗?

其次,由于您使用的是数据库,因此更简单的方法是将函数的 db 结果返回到 DataTable 中,然后使用 Databinding。

【讨论】:

  • 抱歉错误出现在 MessageBox.Show(comboBox1.SelectedValue.ToString()); ...你能说它是什么
  • 如果是对象引用异常,则表示comboBox1中的SelectedValue为null。这意味着该框可能是空的。学习使用调试器。跟踪程序并在触发之前查看 comboBox.Items 中的确切内容。
  • 我的意思是:在添加后的行中添加一个断点。观看 combobox1.Items。在每次循环迭代中,查看值是否在变化。我个人认为这是区分大小写的事情,但这样我们就会发现。或者阅读器返回 0 条记录。
【解决方案4】:
"Object reference not set to an instance of an object for combo box".

Means one or two things normally.
combobox not initialized to null;
or combobox not initalized to new

ComboBox combobox = null;
then inside of the try set the combobox variable to an instance of new like the following 
Try
{
  combobox = new Combobox();
}

【讨论】:

    【解决方案5】:

    我在尝试使用 .SelectedValue 时收到了同样的错误消息。我将其更改为.SelectedItem,错误消失了。示例:

    string selection = comboBox1.SelectedItem.ToString();
    

    【讨论】:

      猜你喜欢
      • 2010-10-06
      • 2013-02-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多