Link to home
Start Free TrialLog in
Avatar of Jimmy Lea
Jimmy LeaFlag for United States of America

asked on

Error reading text files

Hello Experts,

I'm getting an error reading in text files into a listbox.  The problem is in the last line of the text file
which is ||>>>> * End * Of * JDS * File * <<<<||

If I just catch the error, it seems to work fine.  But I need to merge some of these files.  So I think I need to somehow remove that line.
But when I read it I get the error, if I catch it, it doesn't load that line.  What do I do?
BTW this is my first post
Avatar of Dabas
Dabas
Flag of Australia image

Hi JimToGo:
Welcome to EE.
What is the exact error message?

Dabas
Avatar of Mike Tomlinson
Could you also show us how you are reading the file?

~IM
Avatar of Jimmy Lea

ASKER

This is the error:

System.IndexOutOfRangeException: Index was outside the bounds of the array.

From this function

Dim SR As StreamReader = Nothing
        Try
            SR = New StreamReader(fName)
            Dim record As String
            Dim Fields As String()
            Dim NewItem As ListViewItem
            Do While SR.Peek > -1
                record = SR.ReadLine
                Fields = record.Split(",")
                Dim K As Integer = (Fields(0).IndexOf(" - "))
                NewItem = New ListViewItem(Fields(0).Substring(K + 3))
                NewItem.SubItems.Add(Fields(2))
                NewItem.SubItems.Add(Fields(3))
                Dim I As Integer = (Fields(6).IndexOf(":"))
                NewItem.SubItems.Add(Fields(6).Substring(I + 2))
                JDSListView.Items.Add(NewItem)
            Loop
        Catch ex As Exception
            MsgBox(ex.ToString)
        Finally
            SR.Close()
        End Try
In your Catch, have it also show you ex.StackTrace.
Also in your options, Text Editor Basic check the Line numbers option.

it will help to know which line is causing the problem

You can add an if statement after your split statement:

if Ubound(Fields) < 2 then
    'We know that it is the last line
I tried that and still got the error

System.IndexOutOfRangeException: Index was outside the bounds of the array.
at JDSController.frmMain.GetJDSData(StringfName)

It seems to be in the do while sr.peek, I think when it reads that line it throws the exception.

Here is the last code I tried.

Sub GetJDSData(ByVal fName As String)
        Dim SR As StreamReader = Nothing
        Try
            SR = New StreamReader(fName)
            Dim record As String
            Dim Fields As String()
            Dim NewItem As ListViewItem
            Do While SR.Peek > -1
                record = SR.ReadLine()
                Fields = record.Split(",")
                If UBound(Fields) < 2 Then
                    Dim K As Integer = (Fields(0).IndexOf(" - "))
                    NewItem = New ListViewItem(Fields(0).Substring(K + 3))
                    NewItem.SubItems.Add(Fields(2))
                    NewItem.SubItems.Add(Fields(3))
                    Dim I As Integer = (Fields(6).IndexOf(":"))
                    NewItem.SubItems.Add(Fields(6).Substring(I + 2))
                    JDSListView.Items.Add(NewItem)
                End If
            Loop
        Catch ex As Exception
            MsgBox(ex.StackTrace.ToString)
        Finally
            SR.Close()
        End Try
    End Sub
ASKER CERTIFIED SOLUTION
Avatar of Dabas
Dabas
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial

This is the trace with line numbers.
 System.IndexOutOfRangeException: Index was outside the bounds of the array.
   at JDSController.frmMain.GetJDSData(String fName) in C:\VB.Net Projects\JDSController\frmMain.vb:line 691

 Do While SR.Peek > -1
                record = SR.ReadLine
                Fields = record.Split(",")
                If UBound(Fields) < 2 Then
                    Dim K As Integer = (Fields(0).IndexOf(" - "))
                    NewItem = New ListViewItem(Fields(0).Substring(K + 3))
This is line 691                   NewItem.SubItems.Add(Fields(2))
                    NewItem.SubItems.Add(Fields(3))
                    Dim I As Integer = (Fields(6).IndexOf(":"))
                    NewItem.SubItems.Add(Fields(6).Substring(I + 2))
                    JDSListView.Items.Add(NewItem)
                End If
            Loop

If I edit the text file and take that line out, they load without exception.

Also the code above will not load the listbox.  But still throws the exception.  Do I have the if statement in the wrong place?
If I take the If statement out it will load the listbox and throw the exception.  
It seems like it should work with the if statement, because it's throwing the exception when it tries to split into multiple fields.

I appreciate your help.
Got it.

You were right about the if statement I just didn't implement it correctly.
I was trying
 If UBound(Fields) < 2 Then
It should of been this:
If UBound(Fields) > 2 Then

it works fine now.  Thanks for your help
JimToGo:
Glad I could help!
Sometimes it is the simplest bug that eludes us! LOL

Dabas