Jimmy Lea
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
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
Could you also show us how you are reading the file?
~IM
~IM
ASKER
This is the error:
System.IndexOutOfRangeExce ption: 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).Sub string(K + 3))
NewItem.SubItems.Add(Field s(2))
NewItem.SubItems.Add(Field s(3))
Dim I As Integer = (Fields(6).IndexOf(":"))
NewItem.SubItems.Add(Field s(6).Subst ring(I + 2))
JDSListView.Items.Add(NewI tem)
Loop
Catch ex As Exception
MsgBox(ex.ToString)
Finally
SR.Close()
End Try
System.IndexOutOfRangeExce
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).Sub
NewItem.SubItems.Add(Field
NewItem.SubItems.Add(Field
Dim I As Integer = (Fields(6).IndexOf(":"))
NewItem.SubItems.Add(Field
JDSListView.Items.Add(NewI
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
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
ASKER
I tried that and still got the error
System.IndexOutOfRangeExce ption: Index was outside the bounds of the array.
at JDSController.frmMain.GetJ DSData(Str ingfName)
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).Sub string(K + 3))
NewItem.SubItems.Add(Field s(2))
NewItem.SubItems.Add(Field s(3))
Dim I As Integer = (Fields(6).IndexOf(":"))
NewItem.SubItems.Add(Field s(6).Subst ring(I + 2))
JDSListView.Items.Add(NewI tem)
End If
Loop
Catch ex As Exception
MsgBox(ex.StackTrace.ToStr ing)
Finally
SR.Close()
End Try
End Sub
System.IndexOutOfRangeExce
at JDSController.frmMain.GetJ
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).Sub
NewItem.SubItems.Add(Field
NewItem.SubItems.Add(Field
Dim I As Integer = (Fields(6).IndexOf(":"))
NewItem.SubItems.Add(Field
JDSListView.Items.Add(NewI
End If
Loop
Catch ex As Exception
MsgBox(ex.StackTrace.ToStr
Finally
SR.Close()
End Try
End Sub
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
This is the trace with line numbers.
System.IndexOutOfRangeExce
at JDSController.frmMain.GetJ
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).Sub
This is line 691 NewItem.SubItems.Add(Field
NewItem.SubItems.Add(Field
Dim I As Integer = (Fields(6).IndexOf(":"))
NewItem.SubItems.Add(Field
JDSListView.Items.Add(NewI
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.
ASKER
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
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
Glad I could help!
Sometimes it is the simplest bug that eludes us! LOL
Dabas
Welcome to EE.
What is the exact error message?
Dabas