help - System.IndexOutOfRangeException: Index was outside the bounds of the array

dodge_dakota

Newcomer
Joined
Nov 24, 2003
Messages
2
Location
missouri
I create a program in vb .net 2010 express and every time i deploy the program to another pc i get the following message:

Code:
See the end of this message for details on invoking 
just-in-time (JIT) debugging instead of this dialog box.

************** Exception Text **************
System.IndexOutOfRangeException: Index was outside the bounds of the array.
   at Milebug_Report.Form1.Form1_Load(Object sender, EventArgs e)
   at System.EventHandler.Invoke(Object sender, EventArgs e)
   at System.Windows.Forms.Form.OnLoad(EventArgs e)
   at System.Windows.Forms.Form.OnCreateControl()
   at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
   at System.Windows.Forms.Control.CreateControl()
   at System.Windows.Forms.Control.WmShowWindow(Message& m)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
   at System.Windows.Forms.Form.WmShowWindow(Message& m)
   at System.Windows.Forms.Form.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)


************** Loaded Assemblies **************
mscorlib
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.296 (RTMGDR.030319-2900)
    CodeBase: file:///C:/Windows/Microsoft.NET/Framework/v4.0.30319/mscorlib.dll
----------------------------------------
Milebug Report
    Assembly Version: 1.0.0.0
    Win32 Version: 1.0.0.0
    CodeBase: file:///C:/Users/User/AppData/Local/Apps/2.0/E0XKG1ZN.6CB/G22MGT12.EEL/mile..tion_28a1e100699d14cb_0001.0000_5b81ce77ecf97c9e/Milebug%20Report.exe
----------------------------------------
Microsoft.VisualBasic
    Assembly Version: 10.0.0.0
    Win32 Version: 10.0.30319.1 built by: RTMRel
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/Microsoft.VisualBasic/v4.0_10.0.0.0__b03f5f7f11d50a3a/Microsoft.VisualBasic.dll
----------------------------------------
System
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.1001 built by: RTMGDR
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System/v4.0_4.0.0.0__b77a5c561934e089/System.dll
----------------------------------------
System.Core
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.233 built by: RTMGDR
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Core/v4.0_4.0.0.0__b77a5c561934e089/System.Core.dll
----------------------------------------
System.Windows.Forms
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.1002 built by: RTMGDR
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Windows.Forms/v4.0_4.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
----------------------------------------
System.Drawing
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.1001 built by: RTMGDR
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Drawing/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
----------------------------------------
System.Runtime.Remoting
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.1 (RTMRel.030319-0100)
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Runtime.Remoting/v4.0_4.0.0.0__b77a5c561934e089/System.Runtime.Remoting.dll
----------------------------------------
System.Data
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.237 (RTMGDR.030319-2300)
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_32/System.Data/v4.0_4.0.0.0__b77a5c561934e089/System.Data.dll
----------------------------------------
System.Xml
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.233 built by: RTMGDR
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Xml/v4.0_4.0.0.0__b77a5c561934e089/System.Xml.dll
----------------------------------------

************** JIT Debugging **************
To enable just-in-time (JIT) debugging, the .config file for this
application or computer (machine.config) must have the
jitDebugging value set in the system.windows.forms section.
The application must also be compiled with debugging
enabled.

For example:

<configuration>
    <system.windows.forms jitDebugging="true" />
</configuration>

When JIT debugging is enabled, any unhandled exception
will be sent to the JIT debugger registered on the computer
rather than be handled by this dialog box.

I can hit continue and the program will run with out issue. I have search all the JIT issue with no luck for a solution. I've turned on Option strict and the program has no issue on the development PC.

Below is the code where i believe the issue is.

Code:
Public Class Form1
    Dim numberOfFiles As Integer = 0
    Dim ds As New DataSet
    Public TechVan(,) As String = {{"", ""}}
    Dim NumOfVan As Integer = 0

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim x As Integer = 0
        Dim y As Integer = 0
        Dim num_rows As Long = 0
        Dim num_cols As Long = 0
        Dim file_name As String = "TechVan.csv"
        'load tech name and van # into an array
        If File.Exists(file_name) Then
            Dim tmpstream As StreamReader = File.OpenText(file_name)
            Dim strlines() As String
            Dim strline() As String
            'Load content of file to strLines array
            strlines = tmpstream.ReadToEnd().Split(CChar(Environment.NewLine))
            'Redimension the array.
            num_rows = UBound(strlines)
            NumOfVan = CInt(num_rows)
            strline = strlines(0).Split(CChar(","))
            num_cols = UBound(strline)
            ReDim TechVan(CInt(num_rows), CInt(num_cols))
            'Copy the data into the array.
            For x = 0 To CInt(num_rows)
                strline = strlines(x).Split(CChar(","))
                For y = 0 To CInt(num_cols)
                    TechVan(x, y) = strline(y).Trim()
                Next
            Next
        End If
    End Sub


I would greatly appreciate any help given to resolve this issue.
 
Presumably this exception would only be thrown on array access, so let's start by identifying which lines the error might be thrown on.

Code:
Public Class Form1
    Dim numberOfFiles As Integer = 0
    Dim ds As New DataSet
    Public TechVan(,) As String = {{"", ""}}
    Dim NumOfVan As Integer = 0

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim x As Integer = 0
        Dim y As Integer = 0
        Dim num_rows As Long = 0
        Dim num_cols As Long = 0
        Dim file_name As String = "TechVan.csv"
        'load tech name and van # into an array
        If File.Exists(file_name) Then
            Dim tmpstream As StreamReader = File.OpenText(file_name)
            Dim strlines() As String
            Dim strline() As String
            'Load content of file to strLines array
            strlines = tmpstream.ReadToEnd().Split(CChar(Environment.NewLine))
            'Redimension the array.
            num_rows = UBound(strlines)
            NumOfVan = CInt(num_rows)
            [COLOR="Red"]strline = strlines(0).Split(CChar(",")) '1[/COLOR]
            num_cols = UBound(strline)
            ReDim TechVan(CInt(num_rows), CInt(num_cols))
            'Copy the data into the array.
            For x = 0 To CInt(num_rows)
                [COLOR="Red"]strline = strlines(x).Split(CChar(",")) '2[/COLOR]
                For y = 0 To CInt(num_cols)
                    [COLOR="Red"]TechVan(x, y) = strline(y).Trim() ' 3[/COLOR]
                Next
            Next
        End If
    End Sub

Number 1 shouldn't throw an exception, because String.Split should return an array with at least one element. Number 2 shouldn't throw an exception because your code contains a bounds check. That leaves number 3, in which case you're assuming that each line of the file contains at least as many entries as the first line.

Assume the first line has two entries. You re-dim TechVan to have two columns, and y will loop from 0 to 1. Now, if you have a line with only one entry on it, you will attempt to access strline(1), but the array will only contain a single element at strline(0), which would produce an exception.

Hopefully, if you add a check to make sure strline contains the correct number of entries (and log or display an error if this is not the case), it will eliminate the unhandled exception.

For what it's worth, WinForms providing the user the option to keep running the program after an unhandled exception occurs is a very bad idea. When you click "continue", it means that a function ran half-way through before blowing up, and the program could be in an inconsistent state, creating stability issues, and possibly even security issues.
 
snarfblam, thanks for the insite. I found the issue(s)and have resolved them. It helps to have a fresh eyes to look things over and to point one in the right direction.
 
Back
Top