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?