FabioG
FabioG

Reputation: 2986

C# MVC4 Object reference not set to an instance of an object

I'm having an issue that i can't quite understand. I define a model and am using it to create a form, two of it are lists that i would like to use to create checkbox fields. The problem is when I add values to those lists I get an error on defining the model instance.

Let me put some code to clear things.

My model class:

public class Recurso
{
    public int IDRecurso { get; set; }

    [Required(ErrorMessage = "Selecione um tipo de recurso.")]
    [Display(Name = "Tipo Recurso")]
    public int IDTipoRecurso { get; set; }

    public DateTime DataHora { get; set; }

    [Required(ErrorMessage = "Dê um titulo ao recurso.")]
    [Display(Name = "Titulo")]
    public string Titulo { get; set; }

    [Required(ErrorMessage = "Escreva uma breve descrição do recurso.")]
    [Display(Name = "Descrição")]
    public string Descricao { get; set; }

    [Required(ErrorMessage = "Adicione o ficheiro.")]
    [Display(Name = "Recurso")]
    public HttpPostedFileBase Ficheiro { get; set; }
    public string Mime { get; set; }

    [Display(Name = "Associar Recurso")]
    public int Associacao { get; set; }

    [Display(Name = "Tipos de Clientes")]
    public List<int> TiposClientes { get; set; }

    [Display(Name = "Clientes")]
    public List<int> Clientes { get; set; }
}

In the Controler:

public ActionResult AdicionarRecurso()
    {
        Recurso model = new Recurso();

        string email = this.GetEmailFromCookie();
        string key = Admin.GetUserKey(email);

        List<Entidades> clientes = Admin.GetAllClientes(email);
        foreach (Entidades cliente in clientes)
        {
            model.Clientes.Add(cliente.IDEntidade);
        }
        List<TiposClientes> tipos = Admin.GetTiposClientes(email);
        foreach (TiposClientes tipo in tipos)
        {
            model.TiposClientes.Add(tipo.IDTipoCliente);
        }

        return View(model);
    }

This gives me an Object reference not set to an instance of an object error in this line Recurso model = new Recurso();. But it works fine if I remove this part of the code:

List<Entidades> clientes = Admin.GetAllClientes(email);
foreach (Entidades cliente in clientes)
{
    model.Clientes.Add(cliente.IDEntidade);
}
List<TiposClientes> tipos = Admin.GetTiposClientes(email);
foreach (TiposClientes tipo in tipos)
{
    model.TiposClientes.Add(tipo.IDTipoCliente);
}

And I don't understand why.

Also I'm not even sure if it's possible to generate checkbox lists for the model lists Clientes and TiposClientes.

Upvotes: 0

Views: 5508

Answers (1)

Chris
Chris

Reputation: 3298

You need to instantiate the properties Clientes and TiposClientes on your model before calling Add() on those properties, as otherwise they are still null.

Try changing:

List<Entidades> clientes = Admin.GetAllClientes(email);
foreach (Entidades cliente in clientes)
{
    model.Clientes.Add(cliente.IDEntidade);
}
List<TiposClientes> tipos = Admin.GetTiposClientes(email);
foreach (TiposClientes tipo in tipos)
{
    model.TiposClientes.Add(tipo.IDTipoCliente);
}

to

List<Entidades> clientes = Admin.GetAllClientes(email);
model.Clientes = new List<int>(); // Added this line to instantiate the property
foreach (Entidades cliente in clientes)
{
    model.Clientes.Add(cliente.IDEntidade);
}
List<TiposClientes> tipos = Admin.GetTiposClientes(email);
model.TiposClientes = new List<int>(); // Added this line to instantiate the property
foreach (TiposClientes tipo in tipos)
{
    model.TiposClientes.Add(tipo.IDTipoCliente);
}

Upvotes: 2

Related Questions