2016-05-30 3 views
0

したがって、私のコードでNullReferenceExceptionエラーが発生しています。NullReferenceExceptionエラーを再作成できません

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 

人とCreatureNumberは両方公に宣言されている、と私はそれが私がのテキストを変更するコントロールが含まれているユーザーフォームだから前で「シミュレータ」を入れていました。

問題は、2つの異なるユーザーフォームを使用してこの問題を再現しようとしたときに、意図したとおりに機能していたことです。

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

また、番号は公に宣言されました。だから、なぜ上のコードはうまく動作しますが、最初の1つは私にNullReferenceExceptionエラーを与えていますか?私は自分の質問への答えは^〜^

が判明した

+0

可能性のある重複した[?とNullReferenceExceptionは何を、と私はそれをどのように修正すればよい](のhttp://のstackoverflow。 com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) –

答えて

0

、私が編集しようとしていた制御は、最初の例(私にエラーを与えていた1)でパネルにあったが、 2番目にはありません。これに

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

: だから修正は、単にこれを変更することだったの

Simulator.Panel1.Controls(Person & "Creature" & CreatureNumber & "NameLabel").Text = _ 
    CreatureNameComboBox.Text 
関連する問題