NullReferenceException 未处理,对象引用未设置为对象的实例
每当我运行程序时,我都会得到: NullReferenceException 未处理,对象引用未设置为对象的实例。
当我启动程序时,会出现一个名为 MaxScore 的表单,用户可以在其中输入最高分数并按“确定”。 在 OK 事件中,我调用 MainForm 中的方法,以使用为最大分数输入的值作为参数来更新 MainForm 上的 maxGameCountLabel。
收到 NullReferenceException
myGameCountLbl.Text = maxGames.ToString();
当我按“确定”时,我在maxGameCountLblUpdate 方法中
。 这是驻留在 MainForm 中的 maxGameCountLblUpdate 方法代码:
//Update game count label
public void maxGameCountLblUpdate(decimal maxGames)
{
maxGames = decimal.ToInt32(maxGames);
myGameCountLbl.Text = maxGames.ToString();
compGameCountLbl.Text = maxGames.ToString();
}
这是我在 MaxScore 上的 OK 按钮事件:
private void okBtn_Click(object sender, EventArgs e)
{
MainForm.maxGameCountLblUpdate(max);
}
注意,我已经
public Form1 MainForm { get; set; }
在 MaxScore 中
设置了并且我在 MainForm 中创建了 MaxScore:
using (MaxScore scoreForm = new MaxScore())
{
scoreForm.MainForm = this;
scoreForm.ShowDialog();
}
我无法让它工作.. 我已经尝试了很多东西.. 谢谢!
编辑:在 myGameCountLbl.Text = maxGames.ToString(); 添加断点后 myGameCountLbl 似乎出现为空...我很抱歉作为一个新手...我该如何解决这个问题? maxGames 确实为 1,所以这不是问题
Whenever I run my program, I get: NullReferenceException was unhandled, Object Reference not set to an instance of an object.
When I start the program, I have a form appear called MaxScore where the user enters the max score and presses OK. In the OK event, I call a method from MainForm to update the maxGameCountLabel on MainForm with the value entered for the max score, as a parameter.
When I press ok, I get the NullReferenceException at
myGameCountLbl.Text = maxGames.ToString();
of my maxGameCountLblUpdate method.
Here is the maxGameCountLblUpdate method code which resides in MainForm:
//Update game count label
public void maxGameCountLblUpdate(decimal maxGames)
{
maxGames = decimal.ToInt32(maxGames);
myGameCountLbl.Text = maxGames.ToString();
compGameCountLbl.Text = maxGames.ToString();
}
Here is my OK Button event on MaxScore:
private void okBtn_Click(object sender, EventArgs e)
{
MainForm.maxGameCountLblUpdate(max);
}
Note, I have set
public Form1 MainForm { get; set; }
in MaxScore
And I create MaxScore in MainForm with:
using (MaxScore scoreForm = new MaxScore())
{
scoreForm.MainForm = this;
scoreForm.ShowDialog();
}
I can't get this to work.. I have tried many things..
Thanks!
EDIT: After adding a breakpoint at myGameCountLbl.Text = maxGames.ToString();
myGameCountLbl appears to be coming up as null... Im sorry for being a newb... How do I fix this? maxGames, indeed, does come up as 1, so that is not the problem
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
好吧,如果这是导致问题的行:
那么
myGameCountLbl
为空,或者maxGames
为空。 鉴于maxGames
是十进制,这表明myGameCountLbl
为 null。当您对此进行调试并在适当的行上放置断点时会发生什么?
myGameCountLbl
显示什么?Well if this is the line that's causing the problem:
then either
myGameCountLbl
is null, ormaxGames
is. Given thatmaxGames
is a decimal, that suggests thatmyGameCountLbl
is null.What happens when you debug into this and put a breakpoint on the appropriate line? What does that show for
myGameCountLbl
?您是否从构造函数中删除了:
InitializeComponent();
?如果使用设计器构建表单 UI,Visual Studio 会在后台构建一个方法 (Class.designer.cs) 来初始化控件。 如果在访问 UI 元素之前不调用
InitializeComponent()
,您将收到 NullReferenceException。Did you remove:
InitializeComponent();
from the constructor?If you are using the designer to build the form UI, Visual Studio builds a method in the background (Class.designer.cs) to initialize the controls. If you don't call
InitializeComponent()
before you access the UI elements, you will get a NullReferenceException.您还可以让 Visual Studio 中断所有异常,或中断所有 NullReferenceException,然后您可以检查发生了什么。
(调试 -> 异常 -> 公共语言运行时异常...)
You can also have Visual Studio break on all exceptions, or break on all NullReferenceExceptions, and then you can inspect what's going on.
(Debug -> Exceptions -> Common Language Runtime Exceptions ...)
为什么不在该行中放置一个断点并调试两个对象的当前状态? max从哪里来?
MainForm.maxGameCountLblUpdate(max);
Why don't you put a breakpoint in that line and debug the current state of both objects? Where is max coming from in here?
MainForm.maxGameCountLblUpdate(max);
我知道这是很久以前发布的,但我遇到了同样的问题,这就是我解决它的方法。
如果您查看提供的故障排除提示,第二个建议是:
这实际上就是解决方案。 使用 IF 语句检查要分配的值是否不为空,例如:
这将确保您避免将空值分配给 myGameCounLbl
我希望有所帮助
I know this was posted a long time ago but I encountered the same problem and this is how I solved it.
If you look at the troubleshooting tips provided, the second one suggests:
That is actually the solution. Using an IF statement to check if the value to be assigned is not null like:
That will ensure you avoid assigning null value to myGameCounLbl
I hope that helps