C# NullReferenceException 未处理,对象引用未设置为对象的实例

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/866070/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 04:46:48  来源:igfitidea点击:

NullReferenceException was unhandled, Object Reference not set to an instance of an object

c#exceptionreferencenullinstance

提问by Eric

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

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

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

当我按确定时,我在

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 按钮事件:

private void okBtn_Click(object sender, EventArgs e)
{
    MainForm.maxGameCountLblUpdate(max);
}

Note, I have set

注意,我已经设置了

public Form1 MainForm { get; set; }

in MaxScore

在 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,所以这不是问题

采纳答案by dmo

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) 来初始化控件。如果您InitializeComponent()在访问 UI 元素之前不调用,您将收到 NullReferenceException。

回答by Jon Skeet

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

好吧,如果这是导致问题的行:

myGameCountLbl.Text = maxGames.ToString();

then either myGameCountLblis null, or maxGamesis. Given that maxGamesis a decimal, that suggests that myGameCountLblis null.

那么要么myGameCountLbl是空的,要么maxGames是。鉴于这maxGames是一个小数,这表明它myGameCountLbl是空的。

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

当你调试这个并在适当的行上放置一个断点时会发生什么?这说明什么myGameCountLbl

回答by Carlo

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?

为什么不在该行中放置一个断点并调试两个对象的当前状态?max 从哪里来?

MainForm.maxGameCountLblUpdate(max);

MainForm.maxGameCountLblUpdate(max);

回答by jean

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 中断所有异常,或中断所有 NullReferenceException,然后您可以检查发生了什么。

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

(调试 -> 异常 -> 公共语言运行时异常...)

回答by samapco

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.

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

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

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

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

That will ensure you avoid assigning null value to myGameCounLbl

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

I hope that helps

我希望有帮助