|
-
Aug 5th, 2004, 10:27 AM
#1
Thread Starter
New Member
Problem Code
I have no clue why this piece of code doesn't work, It give a Null Reference exception even though the file has a bunch of lines in it. It only goes through once and fails when it actually tries to read the file
Code:
Function FileReader() As ArrayList
Dim oDataStore As System.IO.File
Dim srReader As System.IO.StreamReader
srReader = oDataStore.OpenText("c:\DataStore.txt")
While srReader.Peek <> -1
FileReader.Add(srReader.ReadLine)
End While
srReader.Close()
End Function
-
Aug 5th, 2004, 10:52 AM
#2
I don't know much about VB.Net but your function is called FileReader.
In the middle of the loop your doing FileReader.Add()
Is this valid? should you not create a tmp ArrayList fill that in the loop and then return it.
-
Aug 5th, 2004, 02:58 PM
#3
Lively Member
I believe the following line is giving you the problem: (If not, tell us what line you get the error)
VB Code:
FileReader.Add(srReader.ReadLine)
An instance of FileReader has not been created because you do not return a new array list.
Try this:
VB Code:
Function FileReader() As ArrayList
Dim oDataStore As System.IO.File
Dim srReader As System.IO.StreamReader
Dim FR as New ArrayList 'add this line
srReader = oDataStore.OpenText("c:\DataStore.txt")
While srReader.Peek <> -1
FR.Add(srReader.ReadLine) 'modify this line
End While
srReader.Close()
Return FR 'add this line
End Function
I can do all things with VB.
-
Aug 5th, 2004, 04:09 PM
#4
Thread Starter
New Member
I'll give that a try, thank you. ==> Fixed the problem, Thank you. Would it be possible to ask Why it fixed the problem . Whats the difference between dim FR as New ArrayList and dim FR as ArrayList.
I.E. What does the 'new' modifier do?
Edit: New problem
|
\|/
alPlrDataRaw.CopyTo(alPlrDataSplitRaw)
gives
An unhandled exception of type 'System.ArgumentNullException' occurred in mscorlib.dll
Additional information: Array cannot be null.
Even though the array has 7 entries (Following is the rest of the code and it uses the function which was just fixed by my good friend ThomasJones)
Code:
Private Sub cmdRead_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdRead.Click
Dim alPlrDataRaw As ArrayList
Dim alPlrDataSplitRaw() As Object
' Call The Read File Function and recieve a ArrayList containing a index for each line in the file
alPlrDataRaw = FileReader()
alPlrDataRaw.CopyTo(alPlrDataSplitRaw)
End Sub
Last edited by Tekken; Aug 5th, 2004 at 04:26 PM.
-
Aug 5th, 2004, 06:29 PM
#5
PowerPoster
Originally posted by ThomasJones
I believe the following line is giving you the problem: (If not, tell us what line you get the error)
VB Code:
FileReader.Add(srReader.ReadLine)
An instance of FileReader has not been created because you do not return a new array list.
Try this:
VB Code:
Function FileReader() As ArrayList
Dim oDataStore As System.IO.File
Dim srReader As System.IO.StreamReader
Dim FR as New ArrayList 'add this line
srReader = oDataStore.OpenText("c:\DataStore.txt")
While srReader.Peek <> -1
FR.Add(srReader.ReadLine) 'modify this line
End While
srReader.Close()
Return FR 'add this line
End Function
I'm puzzled by the use of two array lists here. The scope of FR is limited to the life of the function and why use it to return the values? Shouldn't it be something like
VB Code:
Dim FR As New ArrayList 'In the calling event
fr=FileReader
Function FileReader() As ArrayList
Dim oDataStore As System.IO.File
Dim srReader As System.IO.StreamReader
FileReader = New ArrayList
srReader = oDataStore.OpenText("c:\DataStore.txt")
While srReader.Peek <> -1
FileReader.Add(srReader.ReadLine)
End While
srReader.Close()
End Function
Taxes
The more I learn about VB.NET the more I like dBaseIII Plus
The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.
-
Aug 5th, 2004, 10:31 PM
#6
Lively Member
Taxes:
It's a matter of preference. I did that all the time in vb6. I do not like to use functions as variables in the function itself. In most .net code samples that is not done. (atleast from what I've seen)
There is really only one array list because an instance had not been created for FileReader. FR is limited to the scope of the function but when you return it, FileReader becomes a new instance of the value of FR. FR is then no longer addressable but the value has already been transferred.
If I were to do it your way I would make only a small change:
VB Code:
'No need to declare it as new because of the assignment:
Dim FR as ArrayList = FileReader 'In the calling event
'OR
Dim FR as ArrayList
FR = FileReader
Tekken
The difference between:
Dim FR as ArrayList
and
Dim FR as New ArrayList
is that the new creates an actual instance of the object. Without the new the variable is only dimensioned and can only be used if set to an existing instance. The exception would be shared classes.
As far as the error you now have, the problem is the way alPlrDataSplitRaw is declared. I'm not sure if you want to use the copyto() or not but you could declare alPlrDataSplitRaw as an arraylist and pass in alPlrDataRaw in the constructor:
VB Code:
Private Sub cmdRead_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdRead.Click
Dim alPlrDataRaw As ArrayList=FileReader()
Dim alPlrDataSplitRaw As New ArrayList(alPlrDataRaw)
End Sub
or if you want to use the CopyTo() try
VB Code:
Private Sub cmdRead_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdRead.Click
Dim alPlrDataRaw As ArrayList=FileReader()
Dim alPlrDataSplitRaw() As Object = Array.CreateInstance(GetType(Object), alPlrDataRaw.Count) 'Declaring it as Dim alPlrDataSplitRaw as Array is good also
alPlrDataRaw.CopyTo(alPlrDataSplitRaw)
End Sub
I can do all things with VB.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|