System Index out of Range Exception

In UiPath i am using foreach loop with string. for most of the data system works fine. But for following data
I am always getting at the end as :“System Index out of Range Exception”. Data -
0000000 01 0000123.00 0000000 02 0000124.00
0000000 03 0000126.00 0000000 04 0000127.00
0000000 05 0000129.00 0000000 06 0000130.00
0000000 07 0000145.00
Please help

Can you share your logic? On what basis are you creating string array? At what point ur getting that error?

Vaidya, Below url contains Xamal i am just reusing it: Notepad to Excel write. Only difference is above url contains 3 columns here i am using 6 columns. So i have changed column names in build data table & in array row i am passing 6 arrays as {array(0),array(1),…}. All the functionalities are working as expected after inserting the last record i am getting below mentioned error - system index out of range exception.

Bottom line i am reusing above XAMAl with additional columns thats the first change . 2nd change i am using below data:
0000000 01 0000123.00 0000000 02 0000124.00
0000000 03 0000126.00 0000000 04 0000127.00
0000000 05 0000129.00 0000000 06 0000130.00
0000000 07 0000145.00

Thanks for your extended support.

Can you validate your array before interesting into Add Data Row for every record

array.Length>0

or

array.Length=6 if its standard

Added the condition to check array length.

Below data is causing the problem :
0000000 07 0000145.00.
Instead of 6 arrays we are processing 3 columns but irrespective of count I need to process . even after checking array length I am still facing issues with Index out of range exception.

@SKE i think I did in a complex way, but it works. Please find attached logic (used list instead of array). Let me know if doesn’t help.

AddIndex.xaml (7.8 KB)

Vinay list format works . But as per my initial post I have designed the system to accept through arrays - having 6 controls in a row and those were filled by array(0) to array (5) through for each. Now again these list needs to be converted to fixed length arrays (size to 6).

for example below data contains 21 strings as list:

0000000 01 0000123.00 0000000 02 0000124.00
0000000 03 0000126.00 0000000 04 0000127.00
0000000 05 0000129.00 0000000 06 0000130.00
0000000 07 0000145.00

But as per my previous design 4 rows converted in to 4 arrays which fills 21 controls.

List to fixed size conversion requires here please help.

@SKE you are converting each row into list by splitting that row by space delimiter.

Please share your xaml, I will try convert your arrays to list tomorrow. Or someone will be able to help in the meanwhile.Is that Ok?

I am referring our previous XAML that splits through space as an array. Url is available here : Notepad to Excel write .
Because of "system index out of range exception " we went to list to split and store the data. Now the list needs to be converted back in to fixed size array of (5).

Again i am not using row space delimiter - data itself formatted like a row . below data contains 4 rows.
0000000 01 0000123.00 0000000 02 0000124.00
0000000 03 0000126.00 0000000 04 0000127.00
0000000 05 0000129.00 0000000 06 0000130.00
0000000 07 0000145.00

Now list needs to be converted to fixed size array.

Thanks for your extended support.

@ske you cannot add anything to an array.for eg: you last row has only two items but your array size is 5. I tried to validate the length and tried to add empty items to array but could not, so I used the list which can be used downstream.

you can convert your list to array just by adding .ToArray() at the end.

I guess we are on same page.

Yes we are on the same page. Due to this data nature i am facing more issues hope we have started this discussion last week.

What about converting this sting to data table or Excel.Appreciate your help and hope you know the complexities we are facing. please suggest

To convert string to data table, the string need to be formatted into rows and columns.In this scenario I think list will be more useful.

You should be able to build datatable with list, what’s your issue?

Initially we have started writing notepad content to excel :Notepad to Excel write - #7 by SKE . In the above url we constructed data table using the same data and faced the issue thats why i am asking this question Vinay

I don’t have access to computer, we can continue tomorrow after this.

Create a datatable with 6 columns.
string readText = File.ReadAllLines(“yourfile.txt”);
foreach(item in readText)
{
If (item<> string.empty)
{
List = Split(item,“,”).Tolist()
Check list item count here and if less that 6 add empty items here (code I sent)
add data row activity here and in Array row property add below line and datatable variable in datatable

{list(0),list(1),list(2)…list(5)}

}

}

2 Likes

Thanks this resolve the problem.

Glad it worked.