Question Unchecked exception of type system.NullReferenceException'.

BrunoB

Well-known member
Joined
Nov 19, 2022
Messages
96
Programming Experience
Beginner
Hello good evening,I read on stackoverflow that it may be because I did not create any object or because I initialized it but it is not currently initialized.

How can I fix it, it doesn't tell me what line the error is on. thanks.
Program.cs:
class Program
{
static void Main(string[] args)
{

Articulo[] articulos = new Articulo[10];

for (int i = 0; i < 10; i++)
{
Console.WriteLine(" ingese el codigo del art");
articulos[i].codArticulo= int.Parse(Console.ReadLine());
Console.WriteLine(" ingese el precio del art");
articulos[i].precio = float.Parse(Console.ReadLine());
Console.WriteLine(" ingese marca");
articulos[i].codigoMarca = int.Parse(Console.ReadLine());


}

//CON ESTO YA ESTAN CARGADO LOS 10 ART Y EN VEZ DE HACER 3 VECTORES HICIMOS SOLO UNO Y SUS 3 PROPIEDADES
Venta venta = new Venta();
Console.Write("pon cod cliente");
venta.CodigoCliente = int.Parse(Console.ReadLine());


while (venta.CodigoCliente !=0)
{

Console.Write("ingrese venta");
venta.Cantidad = int.Parse(Console.ReadLine());
Console.Write("pon cod art");
venta.CodigoArticulo = int.Parse(Console.ReadLine());
Console.Write("pon cod cliente");
venta.CodigoCliente = int.Parse(Console.ReadLine());

}

}
}
}
 
classventa:
class Venta
{
public int CodigoArticulo { get; set; }
public int Cantidad { get; set; }
public int CodigoCliente { get; set; }

}
}
 
Last edited:
C#:
namespace Ejemplo2

{

    class Articulo

    {     

        private int codMarca;


        public int codArticulo  { get; set; }

        public float precio { get; set; }

        public int codigoMarca

        {

            get { return codMarca; }



            set

            {

                if (value>0 && value<10)

               

                    codMarca = value;



                    else

                        codMarca = -1;

               

            }



        }


    }
 
Last edited:
The compiler cannot tell you because all it's work is at compile time. The error you are getting is a runtime exception. The debugger can tell you if you run your code under the debugger. If you did not change your key mappings, pressing F5 should automatically run your code under the debugger. When the exception is thrown the debugger will take you to the offending line of code -- which should be line 11 of your Main() method.
 
It's like you just ignored everything we said in that other thread. When you create your array, every element of that array is null, i.e. it contains no object. Why would you expect to be able to set a property of an object that doesn't exist? If you want each element of the array to contain an object then you have to create those objects and put them into the array. They don't appear by magic.
 
Take a look at your first three posts in this thread. In the first two, there's no indenting in the code. In the third, there's loads of empty space. If you would like us to help you, the least you can do is help us do so, which would include taking the small amount of time necessary to make sure that your code is formatted for easy reading. It's bad enough reading code like that on a PC but I use my phone sometimes and I know Skydiver does a lot, and reading code like that is real pain.
 
It's like you just ignored everything we said in that other thread. When you create your array, every element of that array is null, i.e. it contains no object. Why would you expect to be able to set a property of an object that doesn't exist? If you want each element of the array to contain an object then you have to create those objects and put them into the array. They don't appear by magic.
It's not that I ignore it, I don't understand 100% what they tell me in many cases, besides the fact that google translator is not 1100% accurate, etc.
I have to say that I am not sure how to use the console,writeline and the user enters the value to be stored in the subindex of each respective array.
The program shows me the console when initializing it, but when entering the number this Null error appears.
 
Take a look at your first three posts in this thread. In the first two, there's no indenting in the code. In the third, there's loads of empty space. If you would like us to help you, the least you can do is help us do so, which would include taking the small amount of time necessary to make sure that your code is formatted for easy reading. It's bad enough reading code like that on a PC but I use my phone sometimes and I know Skydiver does a lot, and reading code like that is real pain.
I made a few small edits, now my code is clearer?thanks
 
This site is not StackOverflow where you go keep going back to massage your original question. This is a chronological forum where posts progress forward. In the future, post updates to your code as replies. Don't edit previous posts to substantially change their content or cause any downstream replies to look weird.
 
If you didn't understand the responses on the other thread, you should have asked.

Anyway here's a crash course. Let's start of with a class for a planet:
C#:
class Planet
{
    public string Name { get; set; }
    public int Size { get; set; }
}

Now recall that the following doesn't work:
C#:
Planet planet;
planet.Name = "Venus";
planet.Size = 12104;

You get the same null object exception, because the variable planet is not actually referencing an instance of a Planet when first declared on line 1. To fix things, you need create an instance of a Planet.
C#:
Planet planet;
planet = new Planet();
planet.Name = "Venus";
planet.Size = 12104;

One way to think of an array is just a series of variables. So instead of having:
C#:
Planet planet0;
Planet planet1;

planet0 = new Planet();
planet0.Name = "Mercury";

planet1 = new Planet();
planet1.Name = "Venus";

Console.WriteLine(planet0.Name);
Console.WriteLine(planet1.Name);

you would use an array:
C#:
Planet planets = new Planet[2];

planet[0] = new Planet();
planet[0].Name = "Mercury";

planet[1] = new Planet();
planet[1].Name = "Venus";

for(int i = 0; i < 2; i++)
{
    Console.WriteLine(planet[i].Name);
}

Notice that for each element of the array, an instance of Planet had to be created and the array element made to point to that instance.
 
The problem was solved by adding this inside the for:
C#:
articles[i] = new Article();
 
Last edited by a moderator:
Which is exactly what the other thread was trying to tell you, as was as earlier in this thread.

As an aside, please post your code in code tags. It's the button that looks like </>. That will protect the formatting of your code, as well as prevent the forum software from thinking that strings in square brackets are BBCodes for formatting text.
 
Back
Top Bottom