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

发布于 2024-07-20 09:59:41 字数 1288 浏览 2 评论 0原文

每当我运行程序时,我都会得到: 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

乱世争霸 2024-07-27 09:59:41

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

myGameCountLbl.Text = maxGames.ToString();

那么 myGameCountLbl 为空,或者 maxGames 为空。 鉴于 maxGames 是十进制,这表明 myGameCountLbl 为 null。

当您对此进行调试并在适当的行上放置断点时会发生什么? myGameCountLbl 显示什么?

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.

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

谎言月老 2024-07-27 09:59:41

您是否从构造函数中删除了: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.

杯别 2024-07-27 09:59:41

您还可以让 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 ...)

清欢 2024-07-27 09:59:41

为什么不在该行中放置一个断点并调试两个对象的当前状态? 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);

清音悠歌 2024-07-27 09:59:41

我知道这是很久以前发布的,但我遇到了同样的问题,这就是我解决它的方法。

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

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

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

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

这将确保您避免将空值分配给 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:

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 (maxGames!=null){
      myGameCountLbl.Text = maxGames.ToString();
}

That will ensure you avoid assigning null value to myGameCounLbl

I hope that helps

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文