我收到以下未处理的异常
NullReferenceException 未处理。你调用的对象是空的。
和警告之类的东西
字段“Project3_MineSweeper.Form3.form2”从未分配给,并且始终具有其默认值 null
这是我的 DB.cs 上的代码
public class DB
{
...
public DataTable GetData()
{
string spName = "GetTime";
Connection.Open();
SqlCommand command = new SqlCommand(spName, Connection);
command.CommandType = CommandType.StoredProcedure;
SqlDataReader reader = command.ExecuteReader();
DataTable dt = new DataTable();
dt.Columns.Add("Name");
dt.Columns.Add("Score");
while (reader.Read())
{
DataRow dr = dt.NewRow();
dr["name"] = Convert.ToString(reader["name"]);
dr["timeScore"] = Convert.ToInt32(reader["timeScore"]);
dt.Rows.Add(dr);
}
Connection.Close();
return dt;
}
}
这是 Form3.cs 的代码
public partial class Form3 : Form
{
//THE WARNING
private Form2 form2;
public Form3()
{
InitializeComponent();
loadData();
}
public void loadData()
{
//UNHANDLED EXCEPTION HERE
DataTable dt2 = form2.db.GetData();
dgvScore.DataSource = dt2;
}
}
最后是 Form2.cs
public partial class Form2 : Form
{
public DB db;
private Form3 form3;
public Form2()
{
db = new DB();
InitializeComponent();
}
...
}
怎么了?我应该怎么做才能解决这个问题?