-4

I am solving a problem in Volleyball. In volleyball sport we have five sets per match.

string[] arrMatchSummary = fromDict.matchsummary.SafeSplit(' ');
string firstSet = arrMatchSummary[0];
string firstSetResult = firstSet.Replace('-', ':');
string secondSet = arrMatchSummary[1];
string secondSetResult = secondSet.Replace('-', ':');
string thirdSet = arrMatchSummary[2];
string thirdSetResult = thirdSet.Replace('-', ':');
string fourthSet = arrMatchSummary[3];
string fourthSetResult = fourthSet.Replace('-', ':');

In arrMatchSummary i have 5 items in array like "25:18" etc.

When match has not started, this line is giving me index out of range exception:

string secondSet = arrMatchSummary[1];

because, when it is not started, there are no second set.

My question is:

Everything works in code, how to SKIP this index out of range and continue so that the program could work?

Thanks.

MMarić
  • 1
  • 3
  • Use arrMatchSummary.Skip(1).Take(1) – jdweng Jan 23 '19 at 10:46
  • 2
    Why skipping the exception when you could make your code saver instead? Check if there are at least 2 items in `arrMatchSummary` before you access `arrMatchSummary[1]`. – Mighty Badaboom Jan 23 '19 at 10:46
  • 1
    Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Adrian Jan 23 '19 at 10:46
  • 1
    I doubt you get a `NullReferenceException` if it contains less than two items but an `IndexOutOfRangeException` – Tim Schmelter Jan 23 '19 at 10:55
  • @Rango you are correct but we cannot see what happens inside that SafeSplit. It is possible that he gets the exception there – Steve Jan 23 '19 at 11:06
  • @Steve: nope, see where he gets the exception: `arrMatchSummary[1]`, so `arrMatchSummary[0]` was no problem. He even mentioned it: _"because, when it is not started, there are no second set."_ I guess the whole code should run only if `!String.IsNullOrEmpty(fromDict.matchsummary)` – Tim Schmelter Jan 23 '19 at 11:07
  • your code is not safe. Use directly an index to get data in an array without check the length of your array is not safe. You have to check the length of your array before do annithing. At least, in for loop, you can create a list of string and add all the result found. List result = new List(); for(int i =0; i < arrMatchSummary.Length; i++){ result.add(arrMatchSummary[i].Replace('-', ':'))} and you get all the result set in the list, or like – Paul Jan 23 '19 at 11:35

3 Answers3

0

Check your variable with If block and if its null or empty simple don't do anything.

Shino Lex
  • 446
  • 3
  • 19
0

Why not use LINQ?

fromDict.matchsummary.SafeSplit(' ')
  .Select(r => r.Replace('-',':'))
  .ToArray();
Michał Turczyn
  • 28,428
  • 14
  • 36
  • 58
0

Try this way:

string[] arrMatchSummary = fromDict.matchsummary.SafeSplit(' ');
for(int i = 0; i < arrMatchSummary.Length; i++)
{
    arrMatchSummary[i] = arrMatchSummary[i].Replace('-', ':');
}
Marco Salerno
  • 4,889
  • 2
  • 8
  • 27