Error: The object reference was not defined as an instance of a C #

0

I want to make a table in console but all done I execute the program the error appears in the method AlunoLista() , already tried to make changes but I can not get there.

     static void Main(string[] args)
        {
            var client = new string[7, 7];
            InsertData<ClientHeader>(client);

            Console.Clear();
            InsertData<ClientHeader>(client);
            AlunoLista(client);
            Console.ReadKey();
        }
        static int getInsertIndex(string[,] matrix)
        {
            for (int j = 0; j < matrix.GetLength(0); j++)
            {
                if (string.IsNullOrEmpty(matrix[j, 0])) return j;
            }

            return -1;
        }
        private static void InsertData<T>(string[,] matrix)
        {

            int n = getInsertIndex(matrix), id = 1;

            matrix[n, 0] = Convert.ToString(id++);
            int x = matrix.GetLength(1) - 1;
            matrix[n, x] = "true";

            for (var j = 1; j < matrix.GetLength(1); j++)
            {
                do
                {
                    Console.Write($"\nInsert {GetHeader<T>(j)}: ");
                    matrix[n, j] = Console.ReadLine();
                } while (string.IsNullOrEmpty(matrix[n, j]));
            }
        }

        private static string GetHeader<T>(int i) => Enum.GetName(typeof(T), i);


        static void AlunoLista(string[,] lista)
        {
            Console.Clear();
            string linha = new String('-', 49);
            int[] tamanho = new int[] { 4, 10, 10, 20,10,10,10 };


            for (int i = 0; i < lista.GetLength(1); i++)
            {
                Console.WriteLine(linha);
                Console.Write("|");
                for (int j = 0; j < lista.GetLength(0); j++)
                {
                    if (lista[j, i] != null) lista[j, i] = "";
                    string espaço =new string (' ', tamanho[j] - lista[j, i].Length);

                    Console.Write($"{lista[j, i]}{espaço}");
                    Console.Write("|");
                }
                Console.WriteLine();
            }
            Console.WriteLine(linha);
        }


        enum ClientHeader { Id, Name, Surname, Addres, CodPostal, Telephone, Email, State };


    }
}
    
asked by anonymous 22.08.2018 / 00:06

1 answer

1

The line before the error is rendering empty string if what it finds is is null, which does not make sense. It makes perfect sense to make this transformation if it is null. Changing this solves the problem. I've improved some things, but I think it can improve more. There is something that is not really ideal, but it may be a requirement.

using static System.Console;

public class Program {
     public static void Main(string[] args) {
         var client = new string[7, 7];
         InsertData<ClientHeader>(client);
         InsertData<ClientHeader>(client);
         AlunoLista(client);
     }
    static int getInsertIndex(string[,] matrix) {
        for (int j = 0; j < matrix.GetLength(0); j++) if (string.IsNullOrEmpty(matrix[j, 0])) return j;
        return -1;
    }
    private static void InsertData<T>(string[,] matrix) {
        int n = getInsertIndex(matrix), id = 1;
        matrix[n, 0] = (id++).ToString();
        int x = matrix.GetLength(1) - 1;
        matrix[n, x] = "true";
        for (var j = 1; j < matrix.GetLength(1); j++) {
            do {
                Write($"\nInsert {GetHeader<T>(j)}: ");
                matrix[n, j] = ReadLine();
            } while (string.IsNullOrEmpty(matrix[n, j]));
        }
    }

    private static string GetHeader<T>(int i) => System.Enum.GetName(typeof(T), i);

    static void AlunoLista(string[,] lista) {
        var linha = new string('-', 49);
        int[] tamanho = new int[] { 4, 10, 10, 20, 10, 10, 10 };
        for (int i = 0; i < lista.GetLength(1); i++) {
            WriteLine($"{linha}|");
            for (int j = 0; j < lista.GetLength(0); j++) {
                lista[j, i] = lista[j, i] ?? "";  // <===================== mudei aqui
                var espaço = new string (' ', tamanho[j] - lista[j, i].Length);
                Write($"{lista[j, i]}{espaço}|");
            }
            WriteLine();
        }
        WriteLine(linha);
    }
    enum ClientHeader { Id, Name, Surname, Addres, CodPostal, Telephone, Email, State };
}

See running on .NET Fiddle . And no Coding Ground . Also I placed GitHub for future reference .

    
22.08.2018 / 00:28