NullReference异常C#(NullReference Exception C#)

编程入门 行业动态 更新时间:2024-10-28 12:16:25
NullReference异常C#(NullReference Exception C#)

我得到以下未处理的异常

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

本文发布于:2023-07-28 21:50:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1309441.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:异常   NullReference   Exception

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!