Results 1 to 5 of 5

Thread: System.IndexOutOfRangeException

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337

    System.IndexOutOfRangeException

    I understand the error but I don't know why I am getting it. I have a tab delimited file that I am reading line by line. The line it fails on in the file is the last one and gives the error

    Code:
    			0000.24c5.eaac		8	Sec1
    	Of00		0007.e788.a2d3 Cam		9	Sec1
    The error points to this line

    RowItem(4) = RowItem(4).TrimEnd(RowItem(4), vbCrLf)

    There is a tab between Cam and 9 and as the same with the line above it.

    Code:
    Dim Contents = IO.File.ReadAllLines("file1.txt").ToList
                Dim Contents2 = IO.File.ReadAllLines("file2.txt").ToList
                Dim StrippedMAC As String
                Dim NewRoom As String
    
                Contents.RemoveAt(0)
                Contents2.RemoveAt(0)
    
               
    
      
                Dim RowItem() As String
    
                For x As Integer = 0 To Contents.Count - 1
                   
                    RowItem = Contents(x).Split(vbTab)
                    RowItem(4) = RowItem(4).TrimEnd(RowItem(4), vbCrLf)  ' <------ Error Line                               
    
                    If RowItem(4).ToString.Length > 1 Then
                        NewRoom = "Room " & RowItem(0).ToString & " " & RowItem(1).ToString
                        StrippedMAC = Replace(CStr(RowItem(4)).ToString, ".", "")
                       
                        For i = 0 To Contents2.Count - 1
                            ' Contents2(i) = Contents2(i).Substring(0, Contents2(i).Count - 1)
                            Dim RowItem2 = Contents2(i).Split(vbTab)
    
                            If Not RowItem2(6).ToString = "NULL" And StrippedMAC = RowItem2(6).ToString Then
                                ' If Not RowItem2(6).ToString = "NULL" Then                           
                                DataGridView1.Rows.Add(RowItem)
                            ElseIf Not NewRoom = RowItem2(7).ToString Or RowItem2(7).ToString = "NULL" Then 
                                DataGridView1.Rows.Add(RowItem)                           
                            End If
                        Next
                    End If
                Next         
            End If
        End Sub

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: System.IndexOutOfRangeException

    Forget what you think is the case and find out what actually is the case. Look at the contents of RowItem and that will tell you where the Tabs were in Contents(x) that it was split on. You can even examine each character in Contents(x) to confirm what they are. If 4 is an invalid index into RowItem then there are fewer than 5 elements in RowItem so there are fewer than 4 Tabs in Contents(x). It's that simple.

    That said, what the hell is this line supposed to achieve anyway?
    vb.net Code:
    1. RowItem(4) = RowItem(4).TrimEnd(RowItem(4), vbCrLf)
    That line is wrong for more than one reason. I suggest that you actually read how TrimEnd works and also ask yourself exactly how a line break could get into that value in the first place.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337

    Re: System.IndexOutOfRangeException

    Ok, I looked into Contents(x) and seen that the space (which is a return character) and errors out. The file is produced from a database and all the returns are suppose to be replaced with spaces, so the text file shows them all on one line but the code thinks its a return still? If I am splitting on a TAB why does it care about spaces?

    And that line was a product of to many copy and pastes lol

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: System.IndexOutOfRangeException

    You need to look at the actual characters. I'm not talking about what they look like in NotePad. I'm talking about their ASCII/Unicode values. Something in that text is not what you think it is.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337

    Re: System.IndexOutOfRangeException

    Yeah, I looked at the same text in a different text editor and it shows they are line breaks. I can see why it errors out. The line just stops so there is no index 4.

    Thanks jmcilhinney, I think I need to look at how that file is produced and find out why its not replacing line break with spaces.

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