Results 1 to 6 of 6

Thread: Problem Code

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2004
    Posts
    7

    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

  2. #2
    Frenzied Member DeadEyes's Avatar
    Join Date
    Jul 2002
    Posts
    1,196
    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.

  3. #3
    Lively Member
    Join Date
    Apr 2003
    Posts
    114
    I believe the following line is giving you the problem: (If not, tell us what line you get the error)

    VB Code:
    1. 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:
    1. Function FileReader() As ArrayList          
    2.   Dim oDataStore As System.IO.File          
    3.   Dim srReader As System.IO.StreamReader          
    4.   Dim FR as New ArrayList  'add this line
    5.   srReader = oDataStore.OpenText("c:\DataStore.txt")          
    6.   While srReader.Peek <> -1              
    7.     FR.Add(srReader.ReadLine)          'modify this line
    8.   End While
    9.   srReader.Close()      
    10.   Return FR  'add this line
    11. End Function
    I can do all things with VB.

  4. #4

    Thread Starter
    New Member
    Join Date
    Aug 2004
    Posts
    7
    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.

  5. #5
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    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:
    1. 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:
    1. Function FileReader() As ArrayList          
    2.   Dim oDataStore As System.IO.File          
    3.   Dim srReader As System.IO.StreamReader          
    4.   Dim FR as New ArrayList  'add this line
    5.   srReader = oDataStore.OpenText("c:\DataStore.txt")          
    6.   While srReader.Peek <> -1              
    7.     FR.Add(srReader.ReadLine)          'modify this line
    8.   End While
    9.   srReader.Close()      
    10.   Return FR  'add this line
    11. 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:
    1. Dim FR As New ArrayList   'In the calling event
    2. fr=FileReader
    3.  
    4. Function FileReader() As ArrayList          
    5.   Dim oDataStore As System.IO.File          
    6.   Dim srReader As System.IO.StreamReader          
    7.   FileReader = New ArrayList  
    8.   srReader = oDataStore.OpenText("c:\DataStore.txt")          
    9.   While srReader.Peek <> -1              
    10.     FileReader.Add(srReader.ReadLine)          
    11.   End While
    12.   srReader.Close()      
    13. 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.

  6. #6
    Lively Member
    Join Date
    Apr 2003
    Posts
    114
    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:
    1. 'No need to declare it as new because of the assignment:
    2. Dim FR as ArrayList = FileReader 'In the calling event
    3. 'OR
    4. Dim FR as ArrayList
    5. 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:
    1. Private Sub cmdRead_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdRead.Click
    2.         Dim alPlrDataRaw As ArrayList=FileReader()
    3.         Dim alPlrDataSplitRaw As New ArrayList(alPlrDataRaw)
    4.     End Sub

    or if you want to use the CopyTo() try
    VB Code:
    1. Private Sub cmdRead_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdRead.Click
    2.         Dim alPlrDataRaw As ArrayList=FileReader()
    3.         Dim alPlrDataSplitRaw() As Object = Array.CreateInstance(GetType(Object), alPlrDataRaw.Count) 'Declaring it as Dim alPlrDataSplitRaw as Array is good also
    4.  
    5.         alPlrDataRaw.CopyTo(alPlrDataSplitRaw)
    6.  
    7.     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
  •  



Click Here to Expand Forum to Full Width