I’m getting a null reference exception in the PropertyGryd, when there are nested collection properties.
Here is example code:
Public Class MyTestBigClass
Public Property FirstProperty As New List(Of MyTestLittleClass)
Public Class MyTestLittleClass
Public Property SecondProperty As New List(Of Integer)
I create a new MyTestBigClass and bind it to a PropertyGrid. I go to that form, click the FirstProperty row, which makes the Collection Editor dialog pop up. I hit « Add » a few times to create some members in the collection. Then I close the dialog. This works well enough so far, and I can confirm that the expected changes to FirstProperty actually occur.
Then I do nothing else, except click the FirstProperty row again. Now, instead of the Collection Editor coming back to show me the members I just added to that collection, I get a NullReferenceException.
Here is the weird part.
The exception does not occur if I change the second-last line to say this instead:
Public Property SecondProperty As List(Of Integer)
That gets rid of the NullReferenceException in this control, and lets me use the bound PropertyGrid just fine. But that’s at the expense of problems elsewhere in the code, because SecondProperty is now most of the time null (aka Nothing in VB).
Worse, that same exception comes back if I initialize SecondProperty anywhere. Even if I make a constructor (a VB New sub) and create the object inside there, somehow having that member be non-null still makes the property grid crash itself.
What is going on? Is this an error in the control code or am I using it wrong? What’s the right way to fix it?