Run-Time Errors

krazykrisi

Member
Joined
Dec 6, 2005
Messages
15
Programming Experience
1-3
I have been sitting here for a half hour staring at this one line of code trying to figure out what it's problem is, it is telling me that it is out of bounds but I don't see the problem. Maybe someone can help? It's the second line that's causing problems.

VB.NET:
[SIZE=2]
[/SIZE][SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][SIZE=2] intIndex = 1 [/SIZE][SIZE=2][COLOR=#0000ff]To[/COLOR][/SIZE][SIZE=2] intListCount [/SIZE][SIZE=2][COLOR=#0000ff]Step[/COLOR][/SIZE][SIZE=2] -1
strRate = lstWeeklyHours.Items(intIndex).SubItems(1).Text
strRate = strRate.Replace("$", "0")
sngRate = Convert.ToSingle(strRate)
strHours = lstWeeklyHours.Items(intIndex).SubItems(2).Text
sngHours = Convert.ToSingle(strHours)
sngTotalHours += sngHours
sngTotalRevenue += sngHours * sngRate
[/SIZE][SIZE=2][COLOR=#0000ff]Next
[/COLOR][/SIZE]
 
Remember Zero based.

The count gives you the actual count, say 50.
The actual location of the 50th item is at (49).

your last item is not at (intIndex) but instead at (intIndex - 1)

lstWeeklyHours.Items(intIndex - 1).SubItems(1).Text
lstWeeklyHours.Items(intIndex - 1).SubItems(2).Text

or

For intIndex = 0 To (intListCount - 1) Step -1

Either way (one or the other - not both) will get you on the right track.
 
It still doesn't like it. I tried both options you gave me. Here is the error I am getting....
error6bc.png


I don't know what it's problem is. Maybe it's just having a bad day so I will try again tomorrow :D
 
If you step through the code do you get the error here?
strHours = lstWeeklyHours.Items(intIndex).SubItems(2).Text

Are there actually 3 SubItems? Right now you are trying to get the 2nd and 3rd Subitems because of the Zerobase.
My next guess is that you want the first and second Subitems.
strHours = lstWeeklyHours.Items(intIndex).SubItems(0).Text
strHours = lstWeeklyHours.Items(intIndex).SubItems(1).Text

As Vis781 states, you need to make sure intIndex &
intListCount are Dim'ed as Integers and intListCount is assigned the value of lstWeeklyHours.Count

We need to see that bit of code, the definition of strHours and info on lstWeeklyHours would help also, otherwise we are just guessing.
 
Last edited:
I didn't actually write this program, I'm just debugging it. Here is all of the code:

VB.NET:
[SIZE=2][COLOR=#0000ff]
Private[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] lstTodaysSales_KeyDown([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Windows.Forms.KeyEventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] lstWeeklyHours.KeyDown
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] intIndex, intListCount [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Integer
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] sngTotalRevenue [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Single[/COLOR][/SIZE][SIZE=2] = 0
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] sngHours [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Single[/COLOR][/SIZE][SIZE=2] = 0
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] sngTotalHours [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Single[/COLOR][/SIZE][SIZE=2] = 0
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] sngRate [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Single[/COLOR][/SIZE][SIZE=2] = 0
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] strRate [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2], strHours [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2], strResults [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Select[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Case[/COLOR][/SIZE][SIZE=2] e.KeyCode
[/SIZE][SIZE=2][COLOR=#0000ff]Case[/COLOR][/SIZE][SIZE=2] Keys.Delete, Keys.D
[/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] lstWeeklyHours.SelectedIndices.Count = 0 [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2]lstWeeklyHours.Items.RemoveAt(lstWeeklyHours.SelectedIndices(0))
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Case[/COLOR][/SIZE][SIZE=2] Keys.Insert, Keys.I
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] objAddHoursForm [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] frmAddEmployee()
objAddHoursForm.Show()
[/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] objAddHoursForm.txtEmployeeName.Text <> "" [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] lviTodaysSales [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] ListViewItem(objAddHoursForm.txtEmployeeName.Text)
lviTodaysSales.SubItems.Add(Format$(objAddHoursForm.nudHours.Value, "G"))
lviTodaysSales.SubItems.Add(Format$(objAddHoursForm.nudRate.Value, "Currency"))
lstWeeklyHours.Items.Add(lviTodaysSales)
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Select
[/COLOR][/SIZE][SIZE=2]intListCount = Convert.ToInt32(lstWeeklyHours.Items.Count)
[/SIZE][SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][SIZE=2] intIndex = 0 [/SIZE][SIZE=2][COLOR=#0000ff]To[/COLOR][/SIZE][SIZE=2] (intListCount - 1) [/SIZE][SIZE=2][COLOR=#0000ff]Step[/COLOR][/SIZE][SIZE=2] -1
strRate = lstWeeklyHours.Items(intIndex).SubItems(1).Text
strRate = strRate.Replace("$", "0")
sngRate = Convert.ToSingle(strRate)
strHours = lstWeeklyHours.Items(intIndex).SubItems(2).Text
sngHours = Convert.ToSingle(strHours)
sngTotalHours += sngHours
sngTotalRevenue += sngHours * sngRate
[/SIZE][SIZE=2][COLOR=#0000ff]Next
[/COLOR][/SIZE][SIZE=2]strResults = Format$(sngTotalRevenue, "Currency")
TextBox1.Text = strResults
TextBox2.Text = Convert.ToString(Format(sngTotalHours, "g"))
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] frmWeeklyReport_Resize([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Object[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]MyBase[/COLOR][/SIZE][SIZE=2].Resize
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] intColumn0Width [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Integer
[/COLOR][/SIZE][SIZE=2]intColumn0Width = [/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].Width - lstWeeklyHours.Columns(1).Width - lstWeeklyHours.Columns(2).Width - 12
[/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] intColumn0Width > 0 [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2]lstWeeklyHours.Columns(0).Width = intColumn0Width
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]
[/COLOR][/SIZE]
 
Take the Step -1 out.... that's used for counting DOWN ... not up... it starts at 0 then goes to -1, which causes the out of bounds error.... No wonder the original code didn't work either....

-tg
 
The way you debug in this situation is you place a breakpoint at the top of the code, then when execution halts at that point you look at the current line and ask yourself "what do I expect to happen when this line of code is executed?". Then you press F10 and you use the Autos, Locals and Watch windows to see if what you expected actually did happen. If not then either your expectation was wrong or the code is wrong. Fix whichever is the problem and then test again. That's debugging. Remember that "divide and conquer" is absolutely the way to solve programming problems. Break a problem up into smaller problems. Solve all the small problems and you have inherently solved the overall problem. Think of each line of code as a smaller problem. Get each line to do the right thing and the program will work. Not always as easy as it sounds but that's the methodology that you need to use.
 
Yeah I did the breakpoint and used the Autos and Watch and Location windows and it told me that for one of the variables (I forgot which one) the value is Nothing. I can't figure out why it is appearing this way.
 
Then you need to work backwards from that point to where you thought an object was going to be assigned to that variable. If you get to where you expected it be assigned and it wasn't then there's your issue. if it was assigned then it's having Nothing assigned to it somewhere else in between.
 
It was sngRate, it's trying to assign strRate to it, why is that not working? strRate should have a value that will be entered by the user I believe.
 
krazykrisi said:
It was sngRate, it's trying to assign strRate to it, why is that not working? strRate should have a value that will be entered by the user I believe.
Only you can say. strRate looks like it's coming from a ListView. You put the data into the ListView and you're getting it out.
 
Back
Top