NullReferenceException未处理,Object Reference未设置为对象的实例

时间:2021-06-03 16:58:22

Whenever I run my program, I get: NullReferenceException was unhandled, Object Reference not set to an instance of an object.

每当我运行我的程序时,我得到:NullReferenceException未处理,Object Reference未设置为对象的实例。

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.

当我启动程序时,我有一个名为MaxScore的表单,用户输入最高分并按OK。在OK事件中,我从MainForm调用一个方法来更新MainForm上的maxGameCountLabel,并将最大分数输入的值作为参数。

When I press ok, I get the NullReferenceException at

当我按下ok时,我得到NullReferenceException

myGameCountLbl.Text = maxGames.ToString();

of my maxGameCountLblUpdate method.

我的maxGameCountLblUpdate方法。

Here is the maxGameCountLblUpdate method code which resides in MainForm:

以下是驻留在MainForm中的maxGameCountLblUpdate方法代码:

//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:

这是我在MaxScore上的OK Button事件:

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:

我在MainForm中创建MaxScore:

    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

编辑:在myGameCountLbl.Text = maxGames.ToString();添加断点后; myGameCountLbl似乎是空的...我很抱歉成为一个新手...我该如何解决这个问题?事实上,maxGames确实是1,所以这不是问题所在

5 个解决方案

#1


Did you remove: InitializeComponent(); from the constructor?

你删除了:InitializeComponent();从构造函数?

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.

如果您使用设计器来构建表单UI,Visual Studio将在后台构建一个方法(Class.designer.cs)来初始化控件。如果在访问UI元素之前未调用InitializeComponent(),则会出现NullReferenceException。

#2


Well if this is the line that's causing the problem:

好吧,如果这是引起问题的那条线:

myGameCountLbl.Text = maxGames.ToString();

then either myGameCountLbl is null, or maxGames is. Given that maxGames is a decimal, that suggests that myGameCountLbl is null.

然后myGameCountLbl为null,或者maxGames为。鉴于maxGames是小数,这表明myGameCountLbl为null。

What happens when you debug into this and put a breakpoint on the appropriate line? What does that show for myGameCountLbl?

调试到此并在适当的行上放置断点时会发生什么?这对myGameCountLbl有什么影响?

#3


You can also have Visual Studio break on all exceptions, or break on all NullReferenceExceptions, and then you can inspect what's going on.

您还可以让Visual Studio中断所有异常,或者中断所有NullReferenceExceptions,然后您可以检查发生了什么。

(Debug -> Exceptions -> Common Language Runtime Exceptions ...)

(Debug - > Exceptions - > Common Language Runtime Exceptions ...)

#4


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);

#5


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:

如果您查看提供的故障排除提示,第二个建议:

Check to determine if the object is null before calling the method.

在调用方法之前,检查以确定对象是否为null。

That is actually the solution. Using an IF statement to check if the value to be assigned is not null like:

这实际上是解决方案。使用IF语句检查要分配的值是否为null,如:

if (maxGames!=null){
      myGameCountLbl.Text = maxGames.ToString();
}

That will ensure you avoid assigning null value to myGameCounLbl

这将确保您避免为myGameCounLbl分配空值

I hope that helps

我希望有所帮助

#1


Did you remove: InitializeComponent(); from the constructor?

你删除了:InitializeComponent();从构造函数?

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.

如果您使用设计器来构建表单UI,Visual Studio将在后台构建一个方法(Class.designer.cs)来初始化控件。如果在访问UI元素之前未调用InitializeComponent(),则会出现NullReferenceException。

#2


Well if this is the line that's causing the problem:

好吧,如果这是引起问题的那条线:

myGameCountLbl.Text = maxGames.ToString();

then either myGameCountLbl is null, or maxGames is. Given that maxGames is a decimal, that suggests that myGameCountLbl is null.

然后myGameCountLbl为null,或者maxGames为。鉴于maxGames是小数,这表明myGameCountLbl为null。

What happens when you debug into this and put a breakpoint on the appropriate line? What does that show for myGameCountLbl?

调试到此并在适当的行上放置断点时会发生什么?这对myGameCountLbl有什么影响?

#3


You can also have Visual Studio break on all exceptions, or break on all NullReferenceExceptions, and then you can inspect what's going on.

您还可以让Visual Studio中断所有异常,或者中断所有NullReferenceExceptions,然后您可以检查发生了什么。

(Debug -> Exceptions -> Common Language Runtime Exceptions ...)

(Debug - > Exceptions - > Common Language Runtime Exceptions ...)

#4


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);

#5


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:

如果您查看提供的故障排除提示,第二个建议:

Check to determine if the object is null before calling the method.

在调用方法之前,检查以确定对象是否为null。

That is actually the solution. Using an IF statement to check if the value to be assigned is not null like:

这实际上是解决方案。使用IF语句检查要分配的值是否为null,如:

if (maxGames!=null){
      myGameCountLbl.Text = maxGames.ToString();
}

That will ensure you avoid assigning null value to myGameCounLbl

这将确保您避免为myGameCounLbl分配空值

I hope that helps

我希望有所帮助