AdvancedHMI Software

General Category => Support Questions => Topic started by: Tped on April 28, 2021, 12:14:26 PM

Title: System.IndexOutOfRangeException: Index was outside the bounds of the array
Post by: Tped on April 28, 2021, 12:14:26 PM
Project is running.  v399x
I'm reading an array of 500 reals and an array of 500 strings.

DataCollectionValue = EthernetIPforCLXCom1.Read("DataCollectionSensors", 500)
DataCollectionName = EthernetIPforCLXCom1.Read("DataCollectionSensorsNames", 500)

No issues.

When I have to download to the plc and the comms die, I get this error:
System.IndexOutOfRangeException: Index was outside the bounds of the array
msgbox has a bunch of text from the Try/Catch, but these two are always present when this happens
EthernetIPforCLXCom.vb Lines 443/364

it never recovers unless I kill the app and restart.  What should I do to make this more robust?  Or is there any way to get the comms back without a restart?
Title: Re: System.IndexOutOfRangeException: Index was outside the bounds of the array
Post by: Archie on April 28, 2021, 12:52:34 PM
Can you try it with v3.99y Beta ?
Title: Re: System.IndexOutOfRangeException: Index was outside the bounds of the array
Post by: Tped on April 28, 2021, 02:49:04 PM
is this the correct file?

https://downloads.advancedhmi.com/AdvancedHMIv399yBeta35R1.zip
Title: Re: System.IndexOutOfRangeException: Index was outside the bounds of the array
Post by: Archie on April 28, 2021, 03:02:28 PM
https://www.advancedhmi.com/forum/index.php?topic=2838.0
Title: Re: System.IndexOutOfRangeException: Index was outside the bounds of the array
Post by: Tped on April 28, 2021, 03:31:26 PM
So far so good, I'll update if I run into issues.  Thanks for the quick reply.
Title: Re: System.IndexOutOfRangeException: Index was outside the bounds of the array
Post by: Tped on April 28, 2021, 03:46:40 PM
it popped an error, but a different one this time.
this application uses a timer to read 2 arrays from the plc every 60 seconds and log the data into a csv file.
it also reads from a spreadsheet and writes recipe data to the plc.

this popped up during one of the timed array reads.  screenshot attached.
Title: Re: System.IndexOutOfRangeException: Index was outside the bounds of the array
Post by: Archie on April 28, 2021, 04:01:57 PM
Is that popup from your code? Do you have your code wrapped in a Try-Catch block?
Title: Re: System.IndexOutOfRangeException: Index was outside the bounds of the array
Post by: Tped on April 28, 2021, 04:37:38 PM
Yes, try catch.  that is the popup from the code.  I'll kill the try catch and post what happens next time it chokes.
Title: Re: System.IndexOutOfRangeException: Index was outside the bounds of the array
Post by: Tped on April 30, 2021, 05:32:07 PM
Archie, it appears to be stable, but still breaks when I download to my plc when making changes.  Is there any way to make it self recover?
Title: Re: System.IndexOutOfRangeException: Index was outside the bounds of the array
Post by: Archie on April 30, 2021, 06:24:12 PM
The drivers do automatically recover from a loss of communication or read error. On the next read or write, it will re-open the connection.
Title: Re: System.IndexOutOfRangeException: Index was outside the bounds of the array
Post by: Tped on April 30, 2021, 06:43:37 PM
seems to be working, i got rid of the msgbox in the catch exception and replaced it with a richtext box so i could track it.
i disconnected the plc and reconnected after if faulted.  It took longer than I expected to recover, but it finally did.  Thanks for the help, i really appreciate it.
Title: Re: System.IndexOutOfRangeException: Index was outside the bounds of the array
Post by: sambutle on September 06, 2022, 01:21:16 AM
This exception means that you're trying to access a collection item by index, using an invalid index. An index is invalid when it's lower than the collection's lower bound or greater than or equal to the number of elements it contains. Indexing an empty list will always throw an exception. Use a method like Add to append the item to the end of the list, or Insert to place the item in the middle of the list somewhere, etc. You cannot index into a c# list (http://csharp.net-informations.com/collection/list.htm) if that offset doesn't exist.  IndexOutOfRangeException exception is thrown as a result of developer error. Instead of handling the exception, you should diagnose the cause of the error and correct your code.

Handling the Exception:

Use for-each loop: This automatically handles indices while accessing the elements of an array.

Use Try-Catch: Consider enclosing your code inside a try-catch statement and manipulate the exception accordingly. As mentioned, C# won