-2

I have tried to read from text a list of integer into jagged array (because its every row contains different number of elements separated by space) but i face System.NullReferenceException on this block of code:

integers [counter] [n] = int.Parse (split[n]);

The whole code:

int[][] integers = new int[200][];
                int[][] tmp = new int[1][];
                int n = 0;
                int counter = 0;
                string[] file = File.ReadAllLines(@"C:\1.txt");
                foreach (string line in file) {

                    string [] split = line.Split(new Char [] {' ', ',', '.', ':', '\t' });

                    foreach (string s in split) {
                        if (s.Trim () != " ") {
                                integers [counter] [n] = int.Parse (split[n]);
                            n++;
                        }

                    }
                    counter++;
                    }
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
Vlad Vv
  • 13
  • 4

1 Answers1

1

You have an array or arrays, but all of those arrays are null. you need to initialize the array before using indexer:

integers[counter] = new int[split.Length];
int index = 0;
foreach (string s in split) 
{
    integers[counter][index] = int.Parse(s);
    index++;
}

Also the condition is redundant.Since you are splitting on space, the split array won't contain any space.

Selman Genç
  • 100,147
  • 13
  • 119
  • 184