Reputation: 338
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
Upvotes: 3
Views: 49767
Reputation: 1
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
Upvotes: 0
Reputation: 4083
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.
Upvotes: 2
Reputation: 1037
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 ...)
Upvotes: 1
Reputation: 25959
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);
Upvotes: 0
Reputation: 1500515
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
?
Upvotes: 4