Reputation: 911
Im getting this error message randomly:
Index was outside the bounds of the array.
And it points to this line:
Dim placename As String = RichTextBox1.Lines(0)
Upvotes: 2
Views: 56942
Reputation: 1
Maybe your SQL Reader didn't get any rows associated with the index you gave. That was the case for me; I was getting columns that weren't in the database.
You could change your code as follows:
Dim placename As String
If RichTextBox1.Lines.Count > 0 Then placename = RichTextBox1.Lines(0)
Upvotes: 0
Reputation: 15055
Index was outside the bounds of the array
That error message usually means that you have called for an object in the array at a location that is null, or has nothing there. It happens in cases like the following;
myArray = [0,1,2,3];
trace(myArray[6]);
As there is nothing in the array at the index 6, it is outside the bounds. If the array is empty at the time of the call, it will give the error for an object at index 0.
I can't tell any more than that by the amount of code that you posted. Try checking to make sure the array has been populated before that line is called.
Upvotes: 1
Reputation: 2923
That means that your RichTextBox1
has no lines in it. Replace that with:
Dim placename As String
If RichTextBox1.Lines.Count() > 0 Then
placename=RichTextBox1.Lines(0)
Else
placename = String.Empty
End if
More Info:
Imagine an array as a street and each element in the array is a house. If there are 30 houses on the street, and I want to find house number 20, I start at the begining (1) and go up until I reach 20. With an array, 0 is where you start instead of 1, so an array with 30 elements, contains indexes 0-29. Now back to the street analogy. Imagine I go onto the street and ask for house number 31. That house doesnt exist because there is only 30 houses. That is effectively what the program is telling you. It is saying 'There are not enough elements in the array for me to get to the one you asked for'. So you asked for the element 0 in the array of lines, effectively saying 'Give me the first line'. Now, if there are 0 lines in the textbox, then the first line does not exist and you will get this error.
Upvotes: 14