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

Asked

Viewed 552 times

0

I wanted to make a table in console but all ran the program appears error in the method AlunoLista(), I’ve tried to make changes but I can’t 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 };


    }
}
  • 1

    Where does it go wrong? You need to give as much information as possible to help.

  • sorry it was not clear the error gives in variable: string space =new string (' ', size[j] - list[j, i].Length); in Alunolist Method

  • Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful for you. You can also vote on any question or answer you find useful on the entire site (when you have 15 points).

1 answer

1

In the line before the error is transforming into string empty if what you find nay is null, which makes no sense. It makes sense to make that transformation if it is null. Changing this solves the problem. I improved some things, but I think I can improve more. There is something that is really not ideal, but it can 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 };
}

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

  • I think there is still some logic error in this code because now when I run "Count" appears it cannot be negative. guess is from subtraction (' ', size[j] - list[j, i]. Length);

  • There must be, but there’s another problem. If you can’t solve open another question by passing on as much information as possible so that it’s easy for people to help you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.