469,903 Members | 1,493 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,903 developers. It's quick & easy.

loading objects in an ArrayList

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
Nov 20 '05 #1
4 1162
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

"Mike" <an*******@discussions.microsoft.com> wrote in message
news:08****************************@phx.gbl...
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

Nov 20 '05 #2
"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

-----Original Message-----
Mike,
Can you post the code you do have?

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

Hope this helps
Jay

"Mike" <an*******@discussions.microsoft.com> wrote in messagenews:08****************************@phx.gbl...
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

.

Nov 20 '05 #3
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
End Sub
Hope this helps
Jay

"Mike R" <mr***@iquest.net> wrote in message
news:00****************************@phx.gbl... "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

-----Original Message-----
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

"Mike" <an*******@discussions.microsoft.com> wrote in

message
news:08****************************@phx.gbl...
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

.

Nov 20 '05 #4
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
Nov 20 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Jens Stjärna | last post: by
14 posts views Thread by vince | last post: by
10 posts views Thread by Jax | last post: by
10 posts views Thread by netnet | last post: by
9 posts views Thread by Scott Cupstid | last post: by
1 post views Thread by Waqarahmed | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.