0

Edit: this is not a duplicate of "what is System.NullReferenceException", I'm aware of what the exception is just not what was causing it in this specific case. However someone already answered in the comments. Thanks!

So I have a program where I need to loop through some Text-boxes, at first I used this,

For Each tb As TextBox In Me.Controls.OfType(Of TextBox)() 
    tb.Enabled = True
    tb.Clear()
Next

Which works like a charm. Problem is that this loops through every single textbox, and now I added new boxex that don't need to end up in this loop. I tried using a list and an array to replace this, but I keep getting the "System.NullReferenceException" exception.

Dim boxList As New List(Of TextBox)(
{
    txtCourse1,
    txtCourse2,
    txtCourse3,
    txtCourse4,
    txtCourse5,
    txtCourse6
})

Here is my list declaration, and here is how I use it,

For Each tb As TextBox In boxList 
    tb.Enabled = True
    tb.Clear()
Next

The values in the text-boxes never seem to enter the loop, hence the exception. Same thing happens with an array, what am I doing wrong? Thanks in advance.

Brian Naranjo
  • 314
  • 2
  • 5
  • 13

0 Answers0