Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List Index Out of Range exception when creating a task

The exact error:

Index was out of range. Must be non-negative and less than the size of the collection.

I've index arrays and lists countless times. I've used for loops with arrays and lists countless times. The data is there, it works. Except when I try to create a task for my function. Mind you, I successfully did this with a foreach loop for a similar function; this new one requires two arguments though, so I can't use a foreach loop properly. At least I don't think I can.

Here is the erroneous code:

if (addressList != null) {
    textBox1.Text += ("Address List Length: " + addressList.Count + Environment.NewLine);

    for (int i = 0; i < addressList.Count; i++) {
        textBox1.Text += ("Task for " + addressList[i] + ":" + portList[i] + " initiated." + Environment.NewLine);

        Task.Factory.StartNew(() => PingTaskAdapted(addressList[i], portList[i]));
    }                
}
else textBox1.Text = ("No IPs have been added.");

Assuming addressList[0] is google.com and portList[0] is 80, Output:

Address List Length: 1
Task for google.com:80 initiated.

then program break, with Visual Studio telling me that at PingTaskAdapted() I'm calling an index that is out of range, when it literally just printed the indexes in question, because they exist.

And just to be clear, if I call PingTaskAdapted(addressList[0], pingList[0]); it works with no issues.

like image 432
soxroxr Avatar asked Jul 12 '17 09:07

soxroxr


People also ask

How do I fix list index out of range?

The Python IndexError: list index out of range can be fixed by making sure any elements accessed in a list are within the index range of the list. This can be done by using the range() function along with the len() function.

What does it mean when it says index was out of range?

Generally, list index out of range means means that you are providing an index for which a list element does not exist.

What is index out of range exception?

An IndexOutOfRangeException exception is thrown when an invalid index is used to access a member of an array or a collection, or to read or write from a particular location in a buffer. This exception inherits from the Exception class but adds no unique members.

What is index out of range in Python?

The error “list index out of range” arises if you access invalid indices in your Python list. For example, if you try to access the list element with index 100 but your lists consist only of three elements, Python will throw an IndexError telling you that the list index is out of range.


1 Answers

Your task will be accessing the list when the task runs. Not sequentially in the line of code you look at in the loop. To make sure that the correct values are captured in the closure (and the lists still exists and has the same values), make local copies outside of the task, that make sure the values are captured at that point in time the loop runs:

var localAddress = addressList[i];
var localPort = portList[i];
Task.Factory.StartNew(() => PingTaskAdapted(localAddress , localPort));
like image 187
nvoigt Avatar answered Oct 16 '22 13:10

nvoigt