NullReferenceException when adding list

Posted by Ishan7 under C# on 2/11/2021 | Points: 10 | Views : 499 | Status : [Member] | Replies : 1
Hello,

I am trying to add something to a list, but I get a System.NullReferenceException: 'Object reference not set to an instance of an object', this is my code:

Module Module1
Structure tList
Dim sgName As String
Dim sgTest As String
Dim stModule As List(Of tModule)
End Structure

Structure tModule
Public sgName As String
Public sgTest As String
End Structure
Sub Main()
Dim l_stModule As New tModule
Dim l_stList As New tList

l_stModule.sgName = "Module"
l_stModule.sgTest = "Test"

l_stList.stModule.Add(l_stModule)
End Sub
End Module


I don't understand how to solve this.




Responses

Posted by: Vuyiswamb on: 3/8/2021 [Member] [MVP] [Administrator] NotApplicable | Points: 25

Up
0
Down
Dint you make a mistake here

l_stList.stModule.Add(l_stModule)

change it to

 l_stList.Add(l_stModule)


Thank you for posting at Dotnetfunda
[Administrator]

Ishan7, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response