Author Topic: System.IndexOutOfRangeException: Index was outside the bounds of the array  (Read 1632 times)

Tped

  • Newbie
  • *
  • Posts: 8
    • View Profile
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?

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5269
    • View Profile
    • AdvancedHMI
Can you try it with v3.99y Beta ?

Tped

  • Newbie
  • *
  • Posts: 8
    • View Profile

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5269
    • View Profile
    • AdvancedHMI

Tped

  • Newbie
  • *
  • Posts: 8
    • View Profile
So far so good, I'll update if I run into issues.  Thanks for the quick reply.

Tped

  • Newbie
  • *
  • Posts: 8
    • View Profile
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.

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5269
    • View Profile
    • AdvancedHMI
Is that popup from your code? Do you have your code wrapped in a Try-Catch block?

Tped

  • Newbie
  • *
  • Posts: 8
    • View Profile
Yes, try catch.  that is the popup from the code.  I'll kill the try catch and post what happens next time it chokes.

Tped

  • Newbie
  • *
  • Posts: 8
    • View Profile
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?

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5269
    • View Profile
    • AdvancedHMI
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.

Tped

  • Newbie
  • *
  • Posts: 8
    • View Profile
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.

sambutle

  • Newbie
  • *
  • Posts: 1
    • View Profile
Re: System.IndexOutOfRangeException: Index was outside the bounds of the array
« Reply #11 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 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