0

I am trying to call a method from the class Reactivo in Program.cs with an array as a parameter.

This is the code calling Reactivo from Program:

    class Program
    {
        static Reactivo r = new Reactivo();

        const int M = 5;
        static Reactivo[] arreglo = new Reactivo[M];

        static void Main(string[] args)
        {
            for (int i = 0; i < M; i++)
            {
               r.ConstructorPorOmision(arreglo[i]);
            }
        }
    }

And this is the method Imprimir(), withing the class Reactivo:

    class Reactivo
    {
        public String color = "";
        public String simbolo = "";
        public int peso = 0;
        public int componentes = 0;

        public void ConstructorPorOmision(Reactivo r)
        {   
            Console.Write("\nValor tipo cadena para el atributo \"simbolo\":\t");
            r.simbolo = Console.ReadLine();
            Console.Write("\nValor tipo cadena para el atributo \"color\":\t");
            r.color = Console.ReadLine();
            Console.Write("\nValor tipo entero para el atributo \"peso\":\t");
            r.peso = Convert.ToInt16(Console.ReadLine());
            do
            {
                if (componentes < 5 || componentes > 25)
                {
                    Console.Write("\nEscribe un valor entre 5 y 25 para componentes");
                 }

                Console.Write("\nValor tipo entero para el atributo \"componentes\":\t");
                r.componentes = Convert.ToInt16(Console.ReadLine());

            } while (componentes < 5 || componentes > 25);

            Console.Write("\nLos valores han sido leidos correctamente.\n");
        }
    }

However, when I try to run the Console application I get the error:

An unhandled exception of type 'System.NullReferenceException' occurred in ConsoleApplication1.exe

I checked for redundancies in my code (for example, call the method inside such method) but I was not able to find one. What seems to be the problem? How can I solve the exception?

epicLevi
  • 1
  • 2
  • 1
    Where, in your code, are you instantiating the instances in the `arreglo` array? You're only declaring the array, not the members of the array. – Enigmativity May 16 '16 at 02:30
  • Adding to what @Enigmativity said, simply add arreglo[i] = new Reactivo(); above r.ConstructorPorOmision(arreglo[i]); – Ash May 16 '16 at 03:06
  • Instantiating the instances by adding arreglo[i] = new Reactivo(); solved the NullReferenceException! Thanks! – epicLevi May 17 '16 at 15:44

0 Answers0