Object reference error

2

When filling a list in a ViewModel, I get the error:

  

Reference to object not established as an instance of an object.

The ViewModel is this:

public class vmMovimientosParking
    {
    public vmMovimientosParking()
    {
        List <tipoPersonal> tipoPersonal = new List<tipoPersonal>();
    }


    public List<Empresa> ListaDepartamento { get; set; }
    public List<MovimientosParking> marcaje { get; set;}

    public List <tipoPersonal> tipoPersonal { get; set; }

}   

Where I call it:

foreach (var m in coches)
            {
                resultadoProvisional.AddRange(marcaje.Where(ma => ma.Matricula == m.MAT).ToList());
                tipoPersonal nuevo = new tipoPersonal();
                nuevo.numEmp = m.NUM;
                nuevo.tipoEmp = m.TIPO;
                nuevo.nombre = m.NOMBRE;
                nuevo.apellidos = m.APELLIDOS;
                modelo.tipoPersonal.Add(nuevo); //Aqui salta el error
            }

And the personal type object:

public class tipoPersonal
{
    public int numEmp;
    public string tipoEmp;
    public string nombre;
    public string apellidos;
}
    
asked by Borja Calvo 29.08.2016 в 13:05
source

1 answer

4

It is clear that the error message occurs because there is an object that is in null , so when you access it fails

I see in the code several places where this could occur, but the first thing I notice is the property marcaje I see that not the instances anywhere, but if you use it when you generate the Where () linq

What happens if you separate the code a bit using

var marcajeList = marcaje.Where(ma => ma.Matricula == m.MAT).ToList()

resultadoProvisional.AddRange(marcajeList);

detects when it fails in which line it stops I have inspected the variables to see which one is in null

The same thing that I said happens with modelo I do not see where the instances, you must also instantiate the property not a new list

public class vmMovimientosParking
{
    public vmMovimientosParking()
    {
        this.tipoPersonal = new List<tipoPersonal>();
    }


    public List<Empresa> ListaDepartamento { get; set; }
    public List<MovimientosParking> marcaje { get; set;}

    public List <tipoPersonal> tipoPersonal { get; set; }

}  

validate how to use this in the constructor

    
answered by 29.08.2016 / 13:17
source