Friday, 27 September 2013

Object reference not set to an instance of an object

Object reference not set to an instance of an object

The purpose of this program is to run through a range of pictureboxes and
set it's .image property to a particular image. I keep getting the error
"Object reference not set to an instance of an object". Coming from the
line that shows "DirectCast(Me.Controls(pic(i)), PictureBox).Image =
My.Resources.glass_buttonred"....Whats weird is if i move that code
outside of the for loop it runs just fine.
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs)
Handles Button1.Click
Dim pic(2) As Object
For i = 0 To 2
pic(i) = "picturebox" + Convert.ToString(i)
DirectCast(Me.Controls(pic(i)), PictureBox).Image =
My.Resources.glass_buttonred
Next
Label1.Text = pic(1)
End Sub

No comments:

Post a Comment