-1

Hello why in this function data could not be read(assigned)? Error is in the line with comments(Object reference not set to an instance of an object.)

protected static int[][] GetMapFromFile(ref int size)
    {         
        using (StreamReader sr = new StreamReader(@"C:\Users\doman\OneDrive\Desktop\Antras semestras\Programavimas\Laboras1\Laboras1\Duomenys.txt"))
        {
            string skyr = " ,.;";
            size = Convert.ToInt32(sr.ReadLine());
            int[][] map = new int[size][];
            for (int i = 0; i < size; i++)
            {
                string line = sr.ReadLine();
                string[] values = line.Split(skyr.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                for (int j = 0; j < values.Length; j++)
                {
                    map[i][j] = Convert.ToInt32(values[j]); // Error here
                    Console.Write(map[i][j]);
                }
                Console.WriteLine();
            }

            return map;
        }
    }

My data file
5
0 1 3 4 2
1 0 4 2 6
3 4 0 7 1
4 2 7 0 7
2 6 1 7 0

D0mm
  • 140
  • 10

1 Answers1

2

You need to create each "sub" array. Add map[i] = new int[values.Length] before the second for loop.

gpro
  • 579
  • 8
  • 17