Why im getting exception: Index was out of range. Must be non-negative and less than the size of the

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
When the process im doing is "Done" im doing:

<pre class="prettyprint _fi = new DirectoryInfo(subDirectoryName).GetFiles("*.bmp");
myTrackPanelss1.trackBar1.Maximum = _fi.Length - 1;[/code]
_fi is FileInfo[]

I used a breakpoint : Maximum is 14023 and the Length of the _fi is 24

Now the process is down and i see the progra mis working good.
Now im trying to move the trackBar1 to move through between the images. 24 images. And now its ok.

Now im doing the process again without quitting the program but this time the process will extract more then 24 images.

So now:

<pre class="prettyprint _fi = new DirectoryInfo(subDirectoryName).GetFiles("*.bmp");
myTrackPanelss1.trackBar1.Maximum = _fi.Length - 1;[/code]
Length is 67 and Maximum is 23<br/>

I used breakpoint so i make continue.

And its working also now. No problems at all.
Now i quit the program and run it again and now im changing the output directory so no images there at all and im doing the process again to a new directory it will extract 67 images.


But sometimes now for example i did it again and got exeption:

<pre class="prettyprint private void trackBar1_Scroll(object sender, EventArgs e)
{
//myTrackPanelss1.trackBar1.Minimum = 0;
myTrackPanelss1.trackBar1.Maximum = _fi.Length - 1; //counter - 1;//list_of_histograms.Count-1;
long[] tt = list_of_histograms[myTrackPanelss1.trackBar1.Value];
histogramControl1.DrawHistogram(tt);
long res = GetTopLumAmount(tt, 1000);
long max = GetHistogramMaximum(tt);
if (res > -1)
label24.Text = (res / 1000.0).ToString();
GetHistogramAverage(tt);
setpicture(myTrackPanelss1.trackBar1.Value);
this.pictureBox1.Refresh();
}[/code]

The exception is on the line: long[] tt = list_of_histograms[myTrackPanelss1.trackBar1.Value];
list_of_histograms containing 67 indexs in each index i have an array of 256 numbers.
When im getting the exception and standinig with the mouse on the list_of_histograms i see : list_of_histograms | Count = 67
And the myTrackPanelss1.trackBar1.Value the Value is 67.

So also the list_of_histograms containing 67 indexs ( each index 256 numbers inside in array ) and also the Value of the trackBar1 is 67.

What could be the problem ? It happen sometimes when im doing a new process in a new empty directory.


Index was out of range. Must be non-negative and less than the size of the collection.<br/>
Parameter name: index


System.ArgumentOutOfRangeException was unhandled<br/>
Message=Index was out of range. Must be non-negative and less than the size of the collection.<br/>
Parameter name: index<br/>
Source=mscorlib<br/>
ParamName=index<br/>
StackTrace:<br/>
at System.ThrowHelper.ThrowArgumentOutOfRangeException()<br/>
at System.Collections.Generic.List`1.get_Item(Int32 index)<br/>
at Extracting_Frames.Form1.trackBar1_Scroll(Object sender, EventArgs e) in D:C-SharpExtracting_FramesExtracting_FramesExtracting_FramesForm1.cs:line 1522<br/>
at System.Windows.Forms.TrackBar.OnScroll(EventArgs e)<br/>
at System.Windows.Forms.TrackBar.WndProc(Message& m)<br/>
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)<br/>
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)<br/>
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)<br/>
at System.Windows.Forms.UnsafeNativeMethods.SendMessage(HandleRef hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)<br/>
at System.Windows.Forms.Control.SendMessage(Int32 msg, IntPtr wparam, IntPtr lparam)<br/>
at System.Windows.Forms.Control.ReflectMessageInternal(IntPtr hWnd, Message& m)<br/>
at System.Windows.Forms.Control.WndProc(Message& m)<br/>
at System.Windows.Forms.ScrollableControl.WmHScroll(Message& m)<br/>
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)<br/>
at System.Windows.Forms.UserControl.WndProc(Message& m)<br/>
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)<br/>
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)<br/>
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)<br/>
at System.Windows.Forms.UnsafeNativeMethods.CallWindowProc(IntPtr wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)<br/>
at System.Windows.Forms.NativeWindow.DefWndProc(Message& m)<br/>
at System.Windows.Forms.Control.DefWndProc(Message& m)<br/>
at System.Windows.Forms.Control.WmMouseMove(Message& m)<br/>
at System.Windows.Forms.Control.WndProc(Message& m)<br/>
at System.Windows.Forms.TrackBar.WndProc(Message& m)<br/>
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)<br/>
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)<br/>
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)<br/>
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)<br/>
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)<br/>
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)<br/>
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)<br/>
at System.Windows.Forms.Application.Run(Form mainForm)<br/>
at Extracting_Frames.Program.Main() in D:C-SharpExtracting_FramesExtracting_FramesExtracting_FramesProgram.cs:line 18<br/>
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)<br/>
at System.AppDomain.nExecuteAssembly(RuntimeAssembly assembly, String[] args)<br/>
at System.Runtime.Hosting.ManifestRunner.Run(Boolean checkAptModel)<br/>
at System.Runtime.Hosting.ManifestRunner.ExecuteAsAssembly()<br/>
at System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext, String[] activationCustomData)<br/>
at System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext)<br/>
at System.Activator.CreateInstance(ActivationContext activationContext)<br/>
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssemblyDebugInZone()<br/>
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)<br/>
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)<br/>
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)<br/>
at System.Threading.ThreadHelper.ThreadStart()<br/>
InnerException:
<hr class="sig danieli

View the full article
 

Similar threads

M
Replies
0
Views
117
Mani25
M
G
Replies
0
Views
162
Graham Skan
G
D
Replies
0
Views
93
Decompressor
D
Back
Top