Note: I understand what a null reference exception is, I just don't understand why I was getting it.
Original:
I have a class, SpecialCases. This class has a property DataArray, which is an array.
I instantiate an object, then I initialize the array, but when I try to set the individual elements of the array I get a null reference exception.
Private Sub btn_AddtoKeyManager_Click(sender As Object, e As EventArgs) Handles btn_AddtoKeyManager.Click
Dim SC_Additem As New SpecialCases
SC_Additem.DataArray() = New String(3) {}
SC_Additem.DataArray() = {cmb_authtypes.SelectedText, txt_URL.Text, txt_Username.Text, txt_password.Text}
End Sub
Turns out I am actually failing to understand something about arrays, because I tried set the values when I initialized and it worked fine:
Private Sub btn_AddtoKeyManager_Click(sender As Object, e As EventArgs) Handles btn_AddtoKeyManager.Click
Dim SC_Additem As New SpecialCases
SC_Additem.DataArray() = New String(3) {cmb_authtypes.SelectedText, txt_URL.Text, txt_Username.Text, txt_password.Text}
End Sub
So after:
SC_Additem.DataArray() = New String(3) {}
The array is initialized with 4 elements, all of which are nothing.
I am then trying to set the each of these elements equal to some value. The array is empty, but I deal with arrays with empty elements all the time.
I am not trying to reference an empty element. I am not doing something like:
SC_Additem.DataArray() = New String(3) {}
aVar = Sc_additem.DataArray(2)
If I did something like that, then I would expect a null reference exception, but I am not doing that.