0

So, I'm running into a NullReferenceException error in my code.

CreatureNumber = CreatureNumberLabel.Text
If YouOrEnemyCreatureLabel.Text = "You" Then
Person = "Your"
End If
If YouOrEnemyCreatureLabel.Text = "Enemy" Then
Person = "Enemy"
End If

'The line below is giving me the error. "_" Added for convenience.
Simulator.Controls(Person & "Creature" & CreatureNumber & "NameLabel").Text = _
   CreatureNameComboBox.Text

Person and CreatureNumber have both been declared publicly, and I had to put "Simulator" in front since that's the userform that contains the control that I want to change the text of.

Now, the problem is that when I tried to recreate this issue (using 2 different userforms), it worked just how I intended it to.

Number = TextBox1.Text
Form2.Controls("Label" & Number).Text = TextBox2.Text

Again, Number was declared publicly. So why is the above code working just fine, but the first one is giving me a NullReferenceException error?

Lord
  • 3
  • 1
  • Possible duplicate of [What is a NullReferenceException, and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Mathieu Guindon May 30 '16 at 20:53

1 Answers1

0

I found the answer to my own question ^~^

Turns out, the control that I was trying to edit was in a Panel in the first example (the one that was giving me the error), but not in the second. So the fix was simply to change this:

Simulator.Controls(Person & "Creature" & CreatureNumber & "NameLabel").Text =_
   CreatureNameComboBox.Text

To this:

Simulator.Panel1.Controls(Person & "Creature" & CreatureNumber & "NameLabel").Text = _
   CreatureNameComboBox.Text
Lord
  • 3
  • 1