So, I've been cruising these and many other forums and have found many who need help concerning this issue, but usually it's resolved using the New statement.

What I'm doing here is I first add a 2D array of PictureBoxes during runtime with the following code

For i = 0 To xVal
            For z = 0 To yVal
                pictures(i, z) = New PictureBox
                currentPoint = New Point(startX + (z * 30), startY + (i * 30))
                pictures(i, z).Location = currentPoint
                pictures(i, z).Size = picSize
                pictures(i, z).BackColor = Color.Black
                pictures(i, z).BorderStyle = BorderStyle.FixedSingle
                pictures(i, z).Name = counter
                counter += 1
            Next
        Next

        For i = 0 To xVal
            For z = 0 To yVal
                Me.Controls.Add(pictures(i, z))
                AddHandler pictures(i, z).Click, AddressOf pictureClick
            Next
        Next

Then later, I have a separate Sub which is supposed to change every picturebox in the array to the same image with this code:

Private Sub All()

        For i = 0 To x
            For j = 0 To y
                pictures(i, j).Image = New Bitmap("images/" & filePic & ".jpg")
            Next
        Next
    End Sub

I know that the first code works because if I coment out Private Sub All() then it works fine, but the line

pictures(i, j).Image = New Bitmap("images/" & filePic & ".jpg")

returns the Object reference not set to an instance of an object error. An alternative code I had was:

Private Sub All()
        Dim index As Integer

        For i = 0 To x
            For j = 0 To y
                grid(index) = filePic
                pictures(i, j).Image = New Bitmap("images/" & filePic & ".jpg")
                index += 1
            Next
        Next
    End Sub

where "grid()" was a string array holding lines of integers for map placement


Any help on this topic would be greatly appreciated... it's been stumping me for a day or so now


Edit: I had this problem in a separate location in my code and while playing around with different solutions I somehow solved it for that instance of the error, but not this one. No idea how :'(

Most likely you have "off-by-one" error while indexing the matrix. Try this in sub All()

For i = 0 To x
  For j = 0 To y
    If i > pictures.GetUpperBound(0) OrElse j > pictures.GetUpperBound(1) Then
      ' Debug
      MessageBox.Show("Out of bounds", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
    End If
    If pictures(i, j) Is Nothing Then
      ' Debug
      MessageBox.Show("Missing picture box, i=" & i.ToString & ", j=" & j.ToString, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
    End If
    pictures(i, j).Image = New Bitmap("images\" & FilePic & ".jpg")
  Next
Next

This should trap (i.e. show a message box) if your code tries to access a non-existing picture box or goes out of bounds.

I changed also pictures(i, j).Image = New Bitmap("images/" & FilePic & ".jpg") to pictures(i, j).Image = New Bitmap("images\" & FilePic & ".jpg") . Windows uses backward slashes, not forward slashes in file paths.

First of all, thanks for the response!

But that didn't work. I know the index is correct, I've used that same method elsewhere in the code.

With that being said, I did try your block to debug and see if it would give me insight into my problem... but it just spit out the same error, this time on the line:

If i > pictures.GetUpperBound(0) OrElse j > pictures.GetUpperBound(1) Then

I really think my problem is in the picturebox array, maybe in the manner in which I declared and specified it.

I actually declare the array as a global variable and then assign it parameters and values in the code block I posted above to avoid having to pass the array from one sub to another if I needed to use it for something.

Thanks for the interest :D

EDIT: Also, before posting, I ran the program and had it pause on the line before it gave me problems so I could see the values being sent to it and they were all within the correct range and in the proper place.

I actually declare the array as a global variable

Nothing wrong with that.

I really think my problem is in the picturebox array, maybe in the manner in which I declared and specified it.

Yes it can be that. Or your code may set the array (matrix actually) to nothing somewhere? Or you have a scope issue i.e. you declare a variable called pictures twice?

Did you check all these values: If i > pictures.GetUpperBound(0) OrElse j > pictures.GetUpperBound(1) Then ?
If you get an error in that line, stop the execution right before the line, open the immediate window (Ctrl-G) and type following (press Enter after each print statement):
? i
? pictures.GetUpperBound(0)
? j
? pictures.GetUpperBound(1)
you can check that values are correct or you do get "object reference..." error from either ? pictures.GetUpperBound(0) or ? pictures.GetUpperBound(1) .

How do you declare pictures array (matrix). With something like

Public pictures(2, 2) As PictureBox ' Global 3x3 matrix

perhaps, or

Public pictures(, ) As PictureBox
.
.
ReDim pictures(2, 2) ' Global 3x3 matrix

Without seeing the whole code, I have to throw all possible causes for that error :)

I think Teme64 is right!

how have you declared your variable "pictures" and where?

maybe from there it will give us further insight into how the variable has been dimensioned.

This article has been dead for over six months. Start a new discussion instead.