Reputation: 31
I'm trying to assign an arrayList a instance of a class but it is giving me the following error.
Object reference not set to an instance of an object.
This is my code that assigns it.
Public memberList As ArrayList
Public Sub LoadUser()
Dim userId As Integer = 0
Dim userData As Data.DataTable = oExecuteSimpleQuery_DT("QA", "SELECT * FROM Member")
If userData IsNot Nothing Then
For Each user As Data.DataRow In userData.Rows
If memberList(userId) Is Nothing Then
memberList(userId) = 1
End If
memberList(userId) = New clsMember(user("UserID"), user("Firstname"), user("Secondname"), user("Username"), user("Password"), user("Email"), user("Rights"))
userId += 1
Next
End If
End Sub
Upvotes: 0
Views: 78
Reputation: 22511
In the code shown in your question you do declare the memberList
variable, but you do not create an instance. In order to solve the error, change the first line to:
Public memberList As New ArrayList()
Two side nodes:
ArrayList
, have a look at List(Of T)
. It offers almost the same functionality, but is strongly typed.NullReferenceException
is a common error that is worth understanding. See this link for details on the reasons and on how to fix them.Upvotes: 3
Reputation: 334
try this:
Public memberList as New List(Of clsMember)
Public Sub LoadUser()
Dim userId As Integer = 1
Dim userData As Data.DataTable = oExecuteSimpleQuery_DT("QA", "SELECT * FROM Member")
If userData IsNot Nothing Then
For Each user As Data.DataRow In userData.Rows
memberList.Add(New clsMember(user("UserID"), user("Firstname"), user("Secondname"), user("Username"), user("Password"), user("Email"), user("Rights"))
userId += 1
Next
End If
End Sub
Upvotes: 1