Jump to content
Xtreme .Net Talk

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


Recommended Posts

Posted

I create a program in vb .net 2010 express and every time i deploy the program to another pc i get the following message:

 

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.

 

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.

  • Leaders
Posted

Presumably this exception would only be thrown on array access, so let's start by identifying which lines the error might be thrown on.

 

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.

[sIGPIC]e[/sIGPIC]

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...