Collection Object Problem

vivek_nz76

Member
Joined
Sep 20, 2005
Messages
8
Programming Experience
3-5
Hi I am adding the dataset to the collection object. It fails here

Public Sub Add(ByVal oUser As CUser)
_userArray.Add(oUser)
End Sub

Says Object reference not set to an instance of an object

PublicFunction GetUserDetail() As CDbUsers
' Create the Database object, using the default database service. The
' default database service is determined through configuration.
Dim db As Database = DatabaseFactory.CreateDatabase()
Dim sqlCommand AsString = "Select * From SecureUser"
Dim dbCommandWrapper As DBCommandWrapper = db.GetSqlStringCommandWrapper(sqlCommand)

Dim userDataSet As DataSet = Nothing
Dim oUser AsNew CUser
Dim userCol AsNew CDbUsers
userDataSet = db.ExecuteDataSet(dbCommandWrapper)

ForEach row As DataRow In userDataSet.Tables(0).Rows
oUser.UserID = row.Item("UserID")
oUser.FName = row.Item("FirstName")
oUser.LName = row.Item("LastName")
oUser.Username = row.Item("Username")
oUser.Password = row.Item("Password")

userCol.Add(oUser) --WHERE I AM CALLING THE ABOVE METHOD
Next

Return userCol

EndFunction

 
Two things:
1) The "Object not set...." error probably happens because the _userarray object hasn't been create (if it has, it isn't obvious from the code given.)
2) YOu keep recycling oUSer in your loop. IMHO that isn't a good thing, because all you do is continualy replace the values in memory. there's no guarantee that you are actualy dealing with a new instance and could endup with a collection of objects that are identical to each other.

-tg
 
Just incase anyone else stumbles in here, there's a difference between declaring and instanciating.

Dim myOBJ as ArrayList <- this is declaring. This will allow you to use the variable in your code, but when it comes to run time, if it hasn't been instanciated, an error will result.
myOBJ = New ArrayList <- This instanciating. This actualy performs the memory allocation and prepares it for use. Once this has happened, then the object is available for use, w/o incurring errors.

-tg
 
Back
Top