-1

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.

ThomasRones
  • 415
  • 5
  • 18
  • On which line is the error coming? – Fabulous Mar 19 '18 at 09:13
  • Sc_Additem.DataArray() = {cmbauthtypes...} – ThomasRones Mar 19 '18 at 09:42
  • Put a breakpoint on that line and inspect the various identifiers in that line. That exception occurs when there is an object is null. Check your controls (unlikely) or the code for the `DataArray` property to ensure it is setting the backing variable – Fabulous Mar 19 '18 at 09:46
  • Can you show your code for the `DataArray` property, unless it's an auto property – Fabulous Mar 19 '18 at 10:32
  • 1
    Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Bugs Mar 19 '18 at 10:36
  • Public Class SpecialCases Property ShowForm As Boolean Property DataArray As String() Property Instructions As String() End Class – ThomasRones Mar 19 '18 at 11:00
  • 1
    It is not valid syntax, the () parentheses should not be there. So one thing that can go wrong is not noticing that the compiler complained about it, then still running an old build of the program that had an NRE bug. – Hans Passant Mar 19 '18 at 11:38
  • ok thanks, I always forget if the () is supposed to be there or not. I usually end up thinking I need it because you declare it as string() and when you refer to an element, you use arrayname(index) – ThomasRones Mar 20 '18 at 14:11

1 Answers1

0

Maybe it depends to your Special Class. Use Property instead of variables in it.

PurTahan
  • 480
  • 7
  • 19
  • He is using properties: https://stackoverflow.com/questions/49358725/vb-net-objects-property-which-is-an-array-null-reference-exception#comment85722668_49358725 – Visual Vincent Mar 19 '18 at 13:27