Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i've got an index out of range exeption on this one

VB
If My.Settings.SavedLinks IsNot Nothing Then
            For Each Sl As String In My.Settings.SavedLinks
                Dim LinkInfo As String = Sl.Split(",").ToString
                Dim NewLink As New ToolStripButton(LinkInfo(0))
                ToolStrip1.Items.Add(NewLink)
                NewLink.Tag = LinkInfo(1)'it gives the error here
                AddHandler NewLink.Click, AddressOf GoToLink
                Dim sep As New ToolStripSeparator
                ToolStrip1.Items.Add(sep)
            Next
        End If


i dont know what im doing wrong but when it loads it give the error

ps
its for a favorite bar in my webbrowser
Posted

Use the debugger:
1) set to stop on throwing exception (Debug --> Exceptions --> .Net Exceptions --> throwing [x])
2) run the program in debug mode
3) when the debugger stops where the execption is thrown, check the message and inspect the individual variables
Cheers
andi
 
Share this answer
 
v2
One of your strings does not contain a ',' character. As a result, there is only one entry in the LinkInfo array, and when you try to access the second one it throws an exception.
Put a breakpoint on the line that throws the error, and look at the value in S1.
 
Share this answer
 
Comments
Bart de Lange 27-Jan-13 10:13am    
no it does not contain any string in my.settings.savedlinks but it gives the error
Bart de Lange 27-Jan-13 10:17am    
and how do i put a breakpoint to a line??
OriginalGriff 27-Jan-13 10:35am    
Easiest way is to put the cursor on the line, then go to the menu: "Debug...Toggle Breakpoint" (it will also show you a short cut key, probably F9). If you click it, it will put a red dot to the left of the line. to show where the breakpoint is. In future, you can just click where the red dot appears to add a breakpoint to that line (now that you know where it is!)

If your settings.SaveLinks contains no strings, then it wouldn't get to the error point, so it contains at least one string - which may be the empty string, just as a "place holder".
If you add a test as the first thing you do in the loop:
If Not String.IsNullOrWhiteSpace(S1) Then
...
End If
Then that would help, as would checking the size of the array before you use it at all:
Dim LinkInfo As String = Sl.Split(",").ToString
If LinkInfo.Length >= 2 Then
...
End If
Bart de Lange 27-Jan-13 10:42am    
thanks it works
OriginalGriff 27-Jan-13 11:11am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900