loading objects in an ArrayList

  • Thread starter Thread starter Mike
  • Start date Start date
M

Mike

I have declared two classes. The first class has 4
private variables. Each has a property defined. I'm
calling a readfile sub from a second class. The second
class also has an Arraylist property.

I sub readfile and dim a new object (inside a loop) of
the first class and pass the four fields to each property
of the first class until the EOF. Within this loop I
want to add each object of the first class into the Array
list of the second class. I keep getting an Object
reference is not an instance of the object when adding to
the array.

I would appreciate any help.

Thanks
 
Mike,
Can you post the code you do have?

Specifically the two classes & the loop, with an indication of where the
exception is occurring.

Hope this helps
Jay
 
"this is piece of code when I load a form to read file
into array.
If OpenFileDialog1.FileName <> "" Then
Dim file1Obj As New clsMovieFile
(OpenFileDialog1.FileName)
file1Obj.ReadFile(file1Obj.FileName)



"First Class
Public Class clsMovie
Private mo_Title As String
Private mo_Year As Integer
Private mo_Category As String
Private mo_Director As String

Public Sub New(ByVal title As String, ByVal year As
Integer, ByVal category As String, ByVal director As
String)
mo_Title = title
mo_Year = year
mo_Category = category
mo_Director = director

End Sub
Public Property MovieTitle() As String
Get
Return mo_Title
End Get
Set(ByVal Value As String)
mo_Title = Value
End Set
End Property

Public Property MovieYear() As Integer
Get
Return mo_Year
End Get
Set(ByVal Value As Integer)
mo_Year = Value
End Set
End Property
Public Property MovieCategory() As String
Get
Return mo_Category
End Get
Set(ByVal Value As String)
mo_Category = Value
End Set
End Property
Public Property MovieDirector() As String
Get
Return mo_Director
End Get
Set(ByVal Value As String)
mo_Director = Value
End Set
End Property
Public Function FormatLine(ByVal formatrec As String)
As String
Dim listMovie As String
listMovie = formatrec.Substring(0, 69) & "|" & _
formatrec.Substring(73, 11) & "|" & _
formatrec.Substring(69, 4) & _
" " & _
formatrec.Substring(84, 25)
Return listMovie
End Function
End Class


"Second Class - below is readfile sub
Public Class clsMovieFile
Private mo_FileName As String
Private mo_RecordCount As Short
Private mo_MovieArr As ArrayList

Public Sub New(ByVal filename As String)
mo_FileName = filename
End Sub
Public Property FileName() As String
Get
Return mo_FileName
End Get
Set(ByVal Value As String)
mo_FileName = Value
End Set
End Property
Public Property RecordCount() As Short
Get
Return mo_RecordCount
End Get
Set(ByVal Value As Short)
mo_RecordCount = Value
End Set
End Property
Public Property MovieArr() As ArrayList
Get
Return mo_MovieArr
End Get
Set(ByVal Value As ArrayList)
mo_MovieArr = Value
End Set
End Property

Public Sub ReadFile(ByVal file As String)

'Try 'Open file and trap any errors.
FileOpen(1, file, OpenMode.Input)
recCount = 0
Dim file2Obj As New clsMovieFile(FileName)
Do Until EOF(1) 'read until end of file and load
list box

Dim movObj As New clsMovie(LineInput
(1).Substring(0, 69), _
LineInput(1).Substring
(69, 4), _
LineInput(1).Substring
(73, 11), _
LineInput(1).Substring
(84, 25))


recCount = recCount + 1
file2Obj.MovieArr.Add(movObj) """This is
where I get an error.

Loop

FileClose(1)

'Catch
MsgBox("Error Opening File: " &
Err.Description, , "Movie Search")
'Finally
FileClose(1)
'End Try

End Sub
End Class
 
Mike,
Within the constructor for clsMovieFile you never initialize the mo_MovieArr
variable
Private mo_MovieArr As ArrayList

Public Sub New(ByVal filename As String)
mo_FileName = filename

mo_MovieArr = New ArrayList

Hope this helps
Jay
 
Hi Mike,

This creates a variable which can take an ArrayList
Private mo_MovieArr As ArrayList
but doesn't, yet.

This allows the caller to get and set the MovieArray
Public Property MovieArr() As ArrayList
Get
Return mo_MovieArr
End Get
Set(ByVal Value As ArrayList)
mo_MovieArr = Value
End Set
End Property
but I wouldn't do it that way...

This accesses the MovieArr Get
file2Obj.MovieArr.Add(movObj)
but no-one's actually created the Arraylist hence the null reference
exception.

As the MovieArray is part of the service provided by the MovieFile, I
wouldn't allow the caller to provide the ArrayList - I would have it created
by the MovieFile and only allow read access.

So I'd rather have

Private mo_MovieArr As New ArrayList


Public Property MovieArr() As ArrayList
Get
Return mo_MovieArr
End Get
End Property

But then I'd consider whether I want to give the entire ArrayList away.
The caller could, for instance, get the ArrayList and replace all the movies
with instances of clsFrog - with strange results sometime later!!

I'd would tend to have an indexed Movie property which allows the
MovieFile to accept and hand out Movies and only Movies. This provides some of
the nature of an ArrayList but hides the actual list away. [One day, when you
cater for huge lists, you might replace it with a HashTable, or even a
database link, etc].

The following is an outline - no error trapping/recovery shown.

Public Property Movie (Index As Integer) As clsMovie
Get
Return mo_MovieArr (Index)
End Get
Set
'Replace the movie at Index with the one given.
If mo_MovieArr.IndexOf (Value) = -1 Then
'Not already in the list
mo_MovieArr (Index) = Value
End If
End Set
End Property

'Add the given Movie if not already in the list.
Public Sub Add (NewMovie As clsMovie)
If Not mo_MovieArr.Contains (NewMovie) Then
mo_MovieArr.Add (NewMovie)
End If
End Sub

Regards,
Fergus
 
Back
Top