简体   繁体   English

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

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

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. 当我启动程序时,出现一个名为MaxScore的表格,用户输入最高分数并按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. 在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上的“确定按钮”事件:

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.Text = maxGames.ToString();添加断点后; myGameCountLbl appears to be coming up as null... Im sorry for being a newb... How do I fix this? myGameCountLbl似乎为null。抱歉,我是新手。如何解决此问题? maxGames, indeed, does come up as 1, so that is not the problem 实际上,maxGames确实为1,所以这不是问题

Well if this is the line that's causing the problem: 好吧,如果这是导致问题的行:

myGameCountLbl.Text = maxGames.ToString();

then either myGameCountLbl is null, or maxGames is. 那么myGameCountLbl为null或maxGames为null。 Given that maxGames is a decimal, that suggests that myGameCountLbl is null. 假设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什么?

Did you remove: InitializeComponent(); 是否删除了: 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. 如果使用设计器生成表单UI,Visual Studio将在后台生成一个方法(Class.designer.cs)以初始化控件。 If you don't call InitializeComponent() before you access the UI elements, you will get a NullReferenceException. 如果在访问UI元素之前未调用InitializeComponent() ,则将收到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. 您还可以使Visual Studio中断所有异常,或者中断所有NullReferenceException,然后检查发生了什么。

(Debug -> Exceptions -> Common Language Runtime Exceptions ...) (调试->异常->通用语言运行时异常...)

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 希望对您有所帮助

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); MainForm.maxGameCountLblUpdate(max);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未处理C#NullReferenceException-未将对象引用设置为对象的实例 - C# NullReferenceException was unhandled - Object reference not set to an instance of an object 未处理的异常:NullReferenceException:对象引用未设置为 - Unhandled Exception: NullReferenceException:Object Reference not set to the NullReferenceException:对象引用未设置为对象#2的实例 - NullReferenceException: Object reference not set to an instance of an object #2 错误:NullReferenceException:未将对象引用设置为对象的实例 - ERROR: NullReferenceException: Object reference not set to an instance of an object NullreferenceException - 对象引用未设置为对象的实例 - NullreferenceException - Object Reference is not set to an instance of a object 处理请求时发生未处理的异常。 NullReferenceException:Object 引用未设置为 object 的实例 - An unhandled exception occurred while processing the request. NullReferenceException: Object reference not set to an instance of an object CSharp未处理的异常:System.NullReferenceException:对象引用未设置为对象的实例 - CSharp Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object 未处理的异常。 System.NullReferenceException:未将对象引用设置为对象的实例 - Unhandled exception. System.NullReferenceException: Object reference not set to an instance of an object C#未处理的异常:System.NullReferenceException:对象引用未设置为对象的实例 - C# Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object 数据存储库实例Null-NullReferenceException吗? 你调用的对象是空的 - Data Repository instance Null - NullReferenceException? Object reference not set to an instance of an object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM