473,387 Members | 1,431 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 1261
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Jens Stjärna | last post by:
Hi. I have a question regarding the ArrayList. In my code I use the arraylist to store objects of certain class. I do not mix object types in the same ArrayList. public ArrayList adresses = new...
14
by: vince | last post by:
Can I add (append) to an xml file that already contains a serialized object, and be able to deserialize to either or both objects from the same file...??? How is this done...?? thanks, vince
10
by: Jax | last post by:
I dont seem to fully comprehend references to objects yet. Lets say for example I do this; Customer c = new Customer(); Customer c1 = c; I understand that if I change c1, I also change c as...
9
by: Ender | last post by:
I have an application that I would like third party developers to be able to create Plug-ins that will be dynamically loaded into our application to extend functionality. I have utilized the...
16
by: RCS | last post by:
So I have an ArrayList that gets populated with objects like: myAL.Add(new CustomObject(parm1,parm2)); I'm consuming this ArrayList from an ObjectDataSource and would like to have this support...
14
by: budy_ludy | last post by:
Hi All, I am new to vb .net, I have an ArrayList and i store class objects in it, and later i want to retrieve each ArrayList items and type cast to the class, How can it be done ? I used...
10
by: netnet | last post by:
I have a collection of objects that I store in the session. Then I look in that collection to see if the collection.contains an object. it answers false. If I dont store that collection in the...
9
by: Scott Cupstid | last post by:
Ok, here it is. Which is more correct? AList = New ArrayList for i = 0 to 10 A = New myObject AList.Add(A) A = nothing next
1
by: BlouHond | last post by:
Hi, we have a VB.net assignment that is completely self study and it is proving quite difficult to do (Not an experienced VB programmer) The programme we have to write is object orientated (with...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.