473,570 Members | 2,844 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1274
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*******@disc ussions.microso ft.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
(OpenFileDialog 1.FileName)
file1Obj.ReadFi le(file1Obj.Fil eName)

"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(ByVa l formatrec As String)
As String
Dim listMovie As String
listMovie = formatrec.Subst ring(0, 69) & "|" & _
formatrec.Subst ring(73, 11) & "|" & _
formatrec.Subst ring(69, 4) & _
" " & _
formatrec.Subst ring(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(Fi leName)
Do Until EOF(1) 'read until end of file and load
list box

Dim movObj As New clsMovie(LineIn put
(1).Substring(0 , 69), _
LineInput(1).Su bstring
(69, 4), _
LineInput(1).Su bstring
(73, 11), _
LineInput(1).Su bstring
(84, 25))
recCount = recCount + 1
file2Obj.MovieA rr.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*******@disc ussions.microso ft.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.n et> 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
(OpenFileDialog 1.FileName)
file1Obj.ReadFi le(file1Obj.Fil eName)

"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(ByVa l formatrec As String)
As String
Dim listMovie As String
listMovie = formatrec.Subst ring(0, 69) & "|" & _
formatrec.Subst ring(73, 11) & "|" & _
formatrec.Subst ring(69, 4) & _
" " & _
formatrec.Subst ring(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(Fi leName)
Do Until EOF(1) 'read until end of file and load
list box

Dim movObj As New clsMovie(LineIn put
(1).Substring(0 , 69), _
LineInput(1).Su bstring
(69, 4), _
LineInput(1).Su bstring
(73, 11), _
LineInput(1).Su bstring
(84, 25))
recCount = recCount + 1
file2Obj.MovieA rr.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*******@disc ussions.microso ft.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.MovieA rr.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.Ind exOf (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.Con tains (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
13940
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 ArrayList(); I store only objects from the class "CAdresses" in this ArrayList. Now, every time I use the objects in the ArrayList I have to type...
14
14286
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
1746
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 they are both referencing the same piece of data. But this isn't always the case it would seem. Sometimes I take a reference out, change it and then...
9
4471
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 "Let Users Add Functionality to Your .NET Applications with Macros and Plug-Ins" article at MSDN for the dynamic loading of DLLs ...
16
6403
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 sorting (because it's ultimately being consumed in a GridView). I can't simply sort the ArrayList (because it only knows it's holding a bunch of...
14
18736
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 CType for casting but it is throwing exception.
10
1886
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 session it answers true. Is the session somehow modifying the collection? The objects look identical.
9
1137
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
1194
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 Product, Customer and Transaction classes). There is also an Access database linked to the programme. How do I load data from my Customer table into...
0
7726
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7636
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7948
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8150
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7700
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6329
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3682
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3671
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
971
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.