1

I'm receiving the following error when running a for loop on Windows. I run the exact same code on macOS or Linux and it runs perfectly fine. The length variable outputs the desired value as well. Appreciate any help in advance!

System.IndexOutOfRangeException: 'Index was outside the bounds of the array.'

```
string getImageIds = coms.WinCli(imageIds);
string[] lineInt = getImageIds.Replace("\r", "").Split('\n');
int length = lineInt.Length;
for (int i = 1; i <= length; i++)
{
    string notWorking = lineInt[i];
    string twistScan = coms.WinCli($"{twistDir} images scan -u {username} -p {password} --address {address} --vulnerability-threshold high {notWorking}");
}```
Ryan Brown
  • 23
  • 1
  • 7
  • 1
    It should be `i < length;` instead of `i <= length;` – dcg Nov 07 '19 at 21:09
  • 2
    You are aware that line endings are often different between platforms, correct? – Duston Nov 07 '19 at 21:10
  • To be environment agnostic regarding to new lines you can use `System.Environment.NewLine` value – dcg Nov 07 '19 at 21:10
  • 3
    In C#, array indexes start at 0, not 1. – Christopher Nov 07 '19 at 21:10
  • 1
    "The two hardest problems in programming are naming variables, cleaning up memory and off-by-one errors." – Christopher Nov 07 '19 at 21:11
  • @Christopher this particular circumstance required me to initialize at 1 rather than 0 because I have to shave off the header column of the output of a command. – Ryan Brown Nov 07 '19 at 21:19
  • *"How to resolve System.IndexOutOfRangeException:"* you make sure its not out of range. – TheGeneral Nov 07 '19 at 21:20
  • @dcg this resolves the error but then it doesn't grab the last value. – Ryan Brown Nov 07 '19 at 21:20
  • 1
    Does this answer your question? [What is an IndexOutOfRangeException / ArgumentOutOfRangeException and how do I fix it?](https://stackoverflow.com/questions/20940979/what-is-an-indexoutofrangeexception-argumentoutofrangeexception-and-how-do-i-f) – germi Nov 07 '19 at 21:30
  • @RyanBrown: If you get this error, there is no (valid) value there. And there never was. Of course I can not remember the lenght having lied before. – Christopher Nov 07 '19 at 21:30
  • I read it up again. Array.Lenght is the count of elements. As indexing starts at 0, anything other the `< Lenght` must cause the exception. Because that is propably the check the framework does on the Indexer. | If you got elements at index 0 and index 1, the Lenght will be 2. – Christopher Nov 07 '19 at 21:31

1 Answers1

1

You can use this:

string getImageIds = coms.WinCli(imageIds);
string[] lineInt = getImageIds.Split(new[] { Environment.NewLine },
                                     StringSplitOptions.None);
int length = lineInt.Length;
for ( int index = 0; index < length; index++ )
{
  string notWorking = lineInt[index];
  string twistScan = coms.WinCli($"{twistDir} images scan -u {username} -p {password} --address {address} --vulnerability-threshold high {notWorking}");
}

I have corrected the split and the loop index because arrays starts from 0 in C#.

If you want to skip the first item, use 1 indeed, but let index < length because the last index is length - 1.

Use StringSplitOptions.RemoveEmptyEntries to remove empty entries.

  • That 1 was intentional, so he could skip over a header row. Got me as well, btw. :) – Christopher Nov 07 '19 at 21:29
  • 1
    The OP did not indicate that, isn't it? –  Nov 07 '19 at 21:30
  • Your awesome! This resolved my issue! I do know arrays start at 0 but this circumstance required me to start at 1 rather than 0 because the first line is column labels from the output of a command. Appreciate the help! – Ryan Brown Nov 07 '19 at 21:32