-3

I am getting the error at getProxy(), the error is index outofbounds.

Error:

An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll
Additional information:
Index was out of range. Must be non-negative and less than the size of the collection.

Code:

static List<String> proxies = new List<String>();


private static String getProxy()
{
    lock (proxies)
    {
        return proxies[new Random().Next(0, proxies.Count)];
    }
}

It is not empty, has a proxy inside, the error is not in my loading function, it is here.

I have added a breakpoint and debugged it, proxies has the value of Count = 3 and proxies.count has the value of 3.

sum1hor
  • 1
  • 3
  • 3
    That's a lot of code, some of which is not relevant, I suspect. Could you cut out the bits that are not likely to be part of the problem? The using/imports come to mind. – GillesDV Jun 14 '16 at 14:46
  • 1
    Whenever you get an exception such as this, let the debugger Break at the exception location and take a look around. This particular exception is likely your indexing going outside of a list/array boundary. – GEEF Jun 14 '16 at 14:50
  • @GillesDV No problem, sorry for that. GEEF I've already done that. – sum1hor Jun 14 '16 at 14:52
  • 4
    If proxies contains no elements at all, then it would be trying to access the first element of an empty list and throw this exception. – GendoIkari Jun 14 '16 at 14:52
  • @GendoIkari It's not empty mate. – sum1hor Jun 14 '16 at 14:53
  • At what line is the error thrown exactly? – GillesDV Jun 14 '16 at 14:55
  • 1
    How do you know it's not empty? Have you put a break point inside the getProxy() method and checked the contents of proxies in there? – GendoIkari Jun 14 '16 at 14:55
  • @GendoIkari Yup i've done that. – sum1hor Jun 14 '16 at 14:56
  • @GillesDV On the return proxy line. – sum1hor Jun 14 '16 at 14:56
  • Possible duplicate of [What is IndexOutOfRangeException and how do I fix it?](http://stackoverflow.com/questions/20940979/what-is-indexoutofrangeexception-and-how-do-i-fix-it) – Sayse Jun 14 '16 at 14:57
  • 2
    1. Why not add a check to ensure it is not empty in your method? `if (proxies.Count == 0) return null;` 2. Your method and data are marked as static, you could have a race condition unless you can reproduce just by yourself. – Igor Jun 14 '16 at 14:57
  • 1
    @sum1hor; I recommend using `var index = new Random().Next(0, proxies.Count); var proxy = proxies[index]; return proxy;`. This will let you see exactly what values you're dealing with. – GendoIkari Jun 14 '16 at 14:58
  • Along with @Igor's suggestion try changing to this as well: `new Random().Next(0, proxies.Count - 1)` because if proxies.Count = 1 and 1 is chosen by the Random it will fail. – Pheonyx Jun 14 '16 at 14:58
  • @Pheonyx No, the second parameter is exclusive. – GendoIkari Jun 14 '16 at 14:59
  • 1
    @GendoIkari really?! Cool, I was convinced it was inclusive. My bad :-) – Pheonyx Jun 14 '16 at 15:00
  • If you have a close vote you should use it since it is obviously unclear what the problem is. OP, you should make an [mcve]. – Sayse Jun 14 '16 at 15:02
  • @GendoIkari proxy doesn't exist in current context – sum1hor Jun 14 '16 at 15:05
  • @sum1hor Not sure what you mean. I'm just suggesting you break up the 1 line of code into 2 or 3 separate lines; so that you can check specifically what value the random statement is returning. – GendoIkari Jun 14 '16 at 15:17

1 Answers1

0

The answer from @Gendolkari:

If proxies contains no elements at all, then it would be trying to access the first element of an empty list and throw this exception.

return proxies[new Random().Next(0, proxies.Count)];

==>

return proxies[new Random().Next(0, 0)];

==>

return proxies[0];

This causes an ArgumentOutOfRangeException as stated in the documentation for List(T).Item because proxies is empty.

  • Additional information: Index was out of range. Must be non-negative and less than the size of the collection. – sum1hor Jun 14 '16 at 15:07
  • I have added a breakpoint and debugged it, proxies has the value of Count = 3 and proxies.count has the value of 3. – sum1hor Jun 14 '16 at 15:13
  • ok, but since this code will crash with an empty list, you will need to change it anyway. See also the advice on using lock https://msdn.microsoft.com/en-gb/library/c5kehkcz.aspx –  Jun 14 '16 at 15:15
  • but why is the list empty? I load the list, then call the getproxy function and lock the list, i really don't understand why it's like this – sum1hor Jun 14 '16 at 15:19
  • this is my first multi threading app if you haven't noticed haha – sum1hor Jun 14 '16 at 15:20
  • @sum1hor - do you lock the list when you change data in the list (remove/add)? If not then you still have a potential race condition. – Igor Jun 14 '16 at 15:27
  • @Igor what i basically did was load the list first, then a function calling to get the proxy after loading and thread amount inputs etc have been entered by user when i call the function it then locks the list etc – sum1hor Jun 14 '16 at 16:45