[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 207
  • Last Modified:

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
0
Jimmy Lea
Asked:
Jimmy Lea
  • 4
  • 4
1 Solution
 
DabasCommented:
Hi JimToGo:
Welcome to EE.
What is the exact error message?

Dabas
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Could you also show us how you are reading the file?

~IM
0
 
Jimmy LeaAuthor Commented:
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
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
DabasCommented:
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
0
 
Jimmy LeaAuthor Commented:
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
0
 
DabasCommented:
JimToGo:
Negative. That would not give you an Index outside the bounds of the array exception.

Maybe the problem is another line?
Try to check that your project is configured to supply debug information.
The stack trace should give you the exact line number of the error.

Dabas
0
 
Jimmy LeaAuthor Commented:

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.
0
 
Jimmy LeaAuthor Commented:
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
0
 
DabasCommented:
JimToGo:
Glad I could help!
Sometimes it is the simplest bug that eludes us! LOL

Dabas
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now