简体   繁体   中英

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.

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

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 ?

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.

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM