我得到以下未处理的异常
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(); } ... }怎么了? 我该怎么做才能解决这个问题?
I am getting the following unhandled exception
NullReferenceException was unhandled. Object reference not set to an instance of an object.
and something like warning
Field 'Project3_MineSweeper.Form3.form2' is never assigned to, and will always have its default value null
Here is the code on my 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; } }And here's the code for 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; } }Lastly Form2.cs
public partial class Form2 : Form { public DB db; private Form3 form3; public Form2() { db = new DB(); InitializeComponent(); } ... }What's wrong? And what should I do to fix this problem?
最满意答案
你永远不会初始化form2,这会导致在这一行上引起null引用:
DataTable dt2 = form2.db.GetData();
你应该做点什么
form2 = new Form2();
根据您的需要而定。
public partial class Form3 : Form { private Form2 form2; // <--- you never initialize form2 public Form3() { InitializeComponent(); loadData(); } public void loadData() { //UNHANDLED EXCEPTION HERE DataTable dt2 = form2.db.GetData(); dgvScore.DataSource = dt2; } }you never initialize form2, which makes it cause a nullreference on this line:
DataTable dt2 = form2.db.GetData();
You should do something like
form2 = new Form2();
Depending on where you need it.
public partial class Form3 : Form { private Form2 form2; // <--- you never initialize form2 public Form3() { InitializeComponent(); loadData(); } public void loadData() { //UNHANDLED EXCEPTION HERE DataTable dt2 = form2.db.GetData(); dgvScore.DataSource = dt2; } }更多推荐
dt,Form,form,public,DB,电脑培训,计算机培训,IT培训"/> <meta name="descri
发布评论