2

I have the following class

public class alumno
{
    public string nombre, matricula;
    public int semestre;

    public string []materias = new string [5];
    public double[] calif = new double[5];
}

And I need to create an array, but I run into an error when accessing it.

static void Main(string[] args)
{
    alumno[] als = new alumno[5];
    alumno al = new alumno(); // here i dont have problem
    al.nombre = "angel"; // here i dont have problem

    als[0].nombre = "angel"; // but here i DO have problem    
    als[0].semestre = 6;
    als[0].matricula = "123"; 
    als[0].materias[0] = "español";
    als[0].calif[0] = 10;
}

The error I receive is "Referencia a objeto no establecida como instancia de un objeto", which in english means "Object reference not set to an instance of an object"

How can I fill my objects on array?

3 Answers 3

2
alumno[] als = new alumno[5]; 

This creates an array that contains 5 elements, but each element must also be created.

als[0] = new alumno();
// now you can access als[0].nombre

You will need to do this for each element in the array before you can access its members.

for (int index = 0; index < als.Length; index++)
{
     als[index] = new alumno();
}

What you have run into is formally known as a NullReferenceException. It is not legal to access the members (properties, methods) of a null reference. When you create an array, each element in the array is set to the default value for the type. For classes, the default value is null, and this is why you have run into your issue.


Unrelated to your direct issue, a few things you should be aware of when writing C# code. It is convention to capitalize class names and public members of classes.

class Alumno
{
     public string Nombre { get; set; }
}

This also highlights another convention: it is not idiomatic in C# to expose member fields (class level variables) to the public. Those are kept hidden. We expose data via properties in C# code (there are exceptions, these are more what you call guidelines than actual rules).

Sign up to request clarification or add additional context in comments.

Comments

0

What's missing?

You have alumno class references, but no alumno class objects.

alumno[0] = new alumno();
alumno[1] = new alumno();
alumno[2] = new alumno();
alumno[3] = new alumno();
alumno[4] = new alumno();

What you created was just reference variables not the alumno objects.

Comments

0

Basically what you are doing there is creating an array of of null values which are able to hold 5 instances of the class alumno.

What you want to do is probably something like

        alumno[] als = new alumno[5];


        //What You Need to Add  
        //VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV

        for (int i = 0; i < als.Length; i++)
        {
            als[i] = new alumno();
        }

        alumno al = new alumno();
        al.nombre = "angel"; 

        als[0].nombre = "angel";  
        als[0].semestre = 6;

        als[0].matricula = "123";

        als[0].materias[0] = "español";
        als[0].calif[0] = 10;

Which will create a new instance of your class within each space in your array

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.