I hope someone can point out to me what I am doing wrong here. I have a set of picture boxes in an array. If I use the following code with the PictureBox array declared in the private sub, there is no issue. If I put the array as part of a public class so that I can access the array from other private functions, I get the Nullreference error. I don't understand why!

Public class
 'declare my array
 Dim Pboxes() As PictureBox = {PB1, PB2, PB3, PB4, PB5, PB6, PB7, PB8}
 Dim TBoxes() As TextBox = {TB1, TB2, TB3, TB4, TB5, TB6, TB7, TB8}

Private Sub Test
'call a function
Call ClearBoxes(Pboxes, TBoxes)
End sub

 Public Sub ClearBoxes(ByRef Pics() As PictureBox, ByRef Txt() As TextBox)
        For i As Integer = 0 To 7
            If Not (Pics(i).Image Is Nothing) Then <<<<<< Nullref at this point
                Pics(i).Image.Dispose()
                Pics(i).Image = Nothing
            End If
            Txt(i).Text = Nothing
        Next i
    End Sub
End class

How did you get away with creating a Class with NO NAME? I just attempted to recreate your code and I had to assume you were using a Form as you had textboxes and pictureboxes and it will not let me in vs2008 sp1 create a class with no name. Anyway, assuming you are using a form You must create a PictureBox Object to test

For i As Integer = 0 To 7
      If Not CType(Pboxes(i), PictureBox) Is Nothing Then
        '      (Pboxes(i).Image Is Nothing) Then '<<<<<< Nullref at this point
        Pics(i).Image.Dispose()
        Pics(i).Image = Nothing
      End If
      Txt(i).Text = Nothing
    Next i

Please disregard my previous post

Public Class Form1
  ''declare my array
  'Dim Pboxes() As PictureBox = {pb1, pb2, pb3, pb4, pb5, pb6, pb7, pb8}

  'Dim TBoxes() As TextBox = {tb1, tb2, tb3, tb4, tb5, tb6, tb7, tb8}

  'Used Lists
  Dim lstPBoxes As New List(Of PictureBox)
  Dim lstTBoxes As New List(Of TextBox)

  Private Sub Test()
    'call a function
    Call ClearBoxes()
  End Sub

  Public Sub ClearBoxes()

    'created so TextBox cooresponding
    'to the PictureBox could be accessed
    Dim i As Integer = 0

    'Loop the PictureBox List
    For Each lp In lstPBoxes
      lp = CType(lp, PictureBox) ' Needed so you have an object to test
      If Not lp.Image Is Nothing Then
        '(Pboxes(i).Image Is Nothing) Then '<<<<<< Nullref at this point
        i = lstPBoxes.IndexOf(lp)
        lp.Image.Dispose()
        lp.Image = Nothing
        CType(lstTBoxes(i), TextBox).Text = Nothing
      End If

    Next

  End Sub

  Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    'Fill PictureBox List
    lstPBoxes.Add(pb1)
    lstPBoxes.Add(pb2)
    lstPBoxes.Add(pb3)
    lstPBoxes.Add(pb4)
    lstPBoxes.Add(pb5)
    lstPBoxes.Add(pb6)
    lstPBoxes.Add(pb7)
    lstPBoxes.Add(pb8)
    lstPBoxes.Add(pb9)

    'Fill TextBox List
    lstTBoxes.Add(tb1)
    lstTBoxes.Add(tb2)
    lstTBoxes.Add(tb3)
    lstTBoxes.Add(tb4)
    lstTBoxes.Add(tb5)
    lstTBoxes.Add(tb6)
    lstTBoxes.Add(tb7)
    lstTBoxes.Add(tb8)


    Call Test()

  End Sub

End Class

thanks! That will do it.

I also found that if I delcare the variables in a public way, I have to initilize them later. This also prevents the issues I was experiencing.

T

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.