c#错误:对象引用未设置为对象的实例

本文关键字:对象 实例 设置 错误 对象引用 | 更新日期: 2023-09-27 18:06:29

我尝试制作一个管理客户联系人的应用程序。形式非常非常简单。有:面板(在这个面板里面有名字,电子邮件等标签)和一个列表框,显示所有保存的联系人在sdf文件。我有一个大问题。我想通过另一个类中的方法刷新列表框内容。我将列表框设置为公共,调试器在项目构建期间没有显示错误。当表单加载时,会提示(通过try-catch异常):"对象引用未设置为对象的实例。"我试着用不同的方式来做这个项目——所有的东西都是在一堂课上写的。下面是代码:

database.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlServerCe;
namespace Contacts
{
    class Database
    {
        public static void RefreshListBox()
        {
            SqlCeConnection connection = null;
                try
                {
                var form1 = Form.ActiveForm as Form1;
                connection = new SqlCeConnection("Datasource = C:''Kontakty.sdf");
                connection.Open();
                SqlCeCommand command = new SqlCeCommand("SELECT * FROM Contacts", connection);
                SqlCeDataReader reader = command.ExecuteReader();
                while (reader.Read())
                {
                    form1.listBox1.Items.Add(reader.GetString(0) + " " + reader.GetString(1));
                }
                }
                catch(Exception exc)
                {
                    MessageBox.Show(exc.Message);
        }
    }
}

Form1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Contacts
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            Database.RefreshListBox();
        }

    }
}

有谁知道是怎么回事吗?

PS: kontakty = contacts(捷克语;-))

c#错误:对象引用未设置为对象的实例

这可能有帮助:

public static void RefreshListBox(ListBox listBox)
  ...
  while (reader.Read())
  {
    listBox.Items.Add(reader.GetString(0) + " " + reader.GetString(1));
  }

然后:

private void Form1_Load(object sender, EventArgs e)
{
  Database.RefreshListBox(this.listBox1);
}
除此之外,你真的不应该将表单或列表框传递到数据库类中。它应该是另一种方式-您的表单应该只是从类中获取数据并填充那里的列表框。

表单。ActiveForm可能是空的,因为From1仍在加载中,可能还不可见,因此不活动

我要做的第一件事是将对列表框的引用传递给RefreshListBox()函数。

没有理由让你的班级有任何知识或参考表格1。

在你的表单中试试:

 private void Form1_Load(object sender, EventArgs e)
 {             
      Database.RefreshListBox(this.listBox1); 
 } 

在你们的课堂上:

public static void RefreshListBox(ListBox listbox)             
{                 
      SqlCeConnection connection = null;
      try
      {
           listbox.Items.Clear()    //I assume you may want to refresh?
           connection = new SqlCeConnection("Datasource = "C:''Kontakty.sdf");
           connection.Open();                     
           SqlCeCommand command = new SqlCeCommand("SELECT * FROM Contacts", connection);
           SqlCeDataReader reader = command.ExecuteReader(); 
           while (reader.Read())   
           {
               listbox.Items.Add(reader.GetString(0) + " " + reader.GetString(1));
           }
      }
      catch(Exception exc)
      {                         
          MessageBox.Show(exc.Message);                  
      }
} 

我省略了很多(比如持久化选中的项目),但是你明白了。

还有-如果你没有指定SQL返回值的顺序,不要使用字段索引。按名称调用字段(我知道您不能使用CE SqlDataReader),或者更好的方法是:在SQL语句中指定它们(及其顺序)。你永远不知道什么时候数据库会被移动或重新生成,默认顺序从"Select * from…"将被修改,再加上如果字段在以后的版本中添加,你只会浪费带宽/数据空间来返回它们的值,如果你不打算使用它们。