Don't get caught up in the word 'List(Of Part)'. A List(Of Part) is just a class, just like Part is a class. The (Of ...) part means that it is a generic class, and the '...' can be replaced by any other type. You could have a List(Of String) or List(Of Integer) or List(Of List(Of Part)).
So, now that you know that a List(Of Part) is just another class, you can apply the same principles that you already know:
If you want to use a Part, you do two things:
1. You declare a variable that holds a Part
2. You assign a new instance of the Part class to that variable.
You can shorten this toCode:Dim p As Part '1. p = New Part() '2.
or evenCode:Dim p As Part = New Part()
which means exactly the same, but is just a little 'syntactic sugar'.Code:Dim p As New Part()
After the first line, all you have is some empty 'space' for a Part to exist in. But there isn't any Part in that space yet. Only after line 2 have you created a new Part and assigned it to the variable.
It works exactly the same with a list:
1. You declare a variable that can hold a List(Of Part)
2. You assign a new instance of the List(Of Part) class to that variable
and again, you can shorten this toCode:Dim parts As List(Of Part) parts = New List(Of Part)
or evenCode:Dim parts As List(Of Part) = New List(Of Part)
and that's what you got.Code:Dim parts As New List(Of Part)
So, you forgot to do step 2: all you had was an 'empty space' (called 'null' as the error mentions), you never assigned a list to that space. Then, you try to call the Add method of 'empty space', and this is what causes the null reference exception (you can't do that on 'nothing' obviously).




Reply With Quote