views:

2362

answers:

4

Hi there, 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

+2  A: 

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?

Jon Skeet
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.
Eric
You'll need to initialize it somewhere - exactly where is hard to say without seeing more of your quote. If you're using the UI designer, I'd expect it to be initialized already.
Jon Skeet
A: 

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

Carlo
+1  A: 

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

jean
+1  A: 

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.

dmo
Wow! I had InitializeComponent() after all of my other code... Ahhh! Thank you very much, putting it back at the top fixed it!
Eric
I'd expect the rest of the form not to work in that case, which doesn't sound like the initial description.
Jon Skeet