-2

I have the following viewmodel with a constructor:

public class ActivitiesReportViewModel
{

    public ActivitiesReportViewModel()
    {
        List<ActivityType> ActivityTypeList = new List<ActivityType>();
        List<Activity> ActivityList = new List<Activity>();
        List<Client> ClientList = new List<Client>();
        List<List<ActivityCounter>> ActivityCounterList = new List<List<ActivityCounter>>();
    }

    [DisplayName("Responsável")]
    public string UserName { get; set; }

    [DisplayName("Data Inicial")]
    [Required]
    public DateTime DateFrom { get; set; }

    [DisplayName("Data Final")]
    [Required]
    public DateTime DateTo { get; set; }

    [DisplayName("Tipos de Atividade")]
    public virtual List<ActivityType> ActivityTypeList { get; set; }

    [DisplayName("Atividades")]
    public virtual List<Activity> ActivityList { get; set; }

    [DisplayName("Clientes")]
    public virtual List<Client> ClientList { get; set; }

    public List<List<ActivityCounter>> ActivityCounterList { get; set; }

}

And my ActionResult:

 public ActionResult ActivityReport()
    {
      var model = new ActivitiesReportViewModel();
      return View(model);
    }

But after initializing model, all lists are null as bellow:

debug showing null list

I debugged the execution of the code and seems like the constructor is being correctly called, I just can't understand why all lists are still null after this.

I'm new to C#, so maybe I'm just missing something basic here and will be very grateful for any help you could provide.

Thank you in advance.

Ps: the problem with this situation is that I get a null refference exception because my lists are null when used in my view.

  • Hello techspider! All lists will be populated after the user sets the filters in my view and submit it to the other ActionResult ( ActionResult ActivityReport(ActivitiesReportViewModel model)). In this case I just want to show the view with no data on it. Thanks! – Diego Gomes Santos May 09 '16 at 19:51

4 Answers4

3

You're declaring new local variables in the constructor and assigning to them, not to the properties in your class.

Change the constructor to:

public ActivitiesReportViewModel()
{
    ActivityTypeList = new List<ActivityType>();
    ActivityList = new List<Activity>();
    ClientList = new List<Client>();
    ActivityCounterList = new List<List<ActivityCounter>>();
}

Duplicate names like that is allowed, but can make things confusing.

Chris
  • 5,442
  • 17
  • 30
0

You've not assigned any values within your constructor. You need to add another constructor with all your fields and then assign them. Then add the fields in your instantiation

TechnicalTophat
  • 1,655
  • 1
  • 15
  • 37
0

Your initialization is wrong. You are creating another variable instead of instantiating instance members

    public class ActivitiesReportViewModel
{

    public ActivitiesReportViewModel()
    {
        ActivityTypeList = new List<ActivityType>();
        ActivityList = new List<Activity>();
        ClientList = new List<Client>();
        ActivityCounterList = new List<List<ActivityCounter>>();
    }

    [DisplayName("Responsável")]
    public string UserName { get; set; }

    [DisplayName("Data Inicial")]
    [Required]
    public DateTime DateFrom { get; set; }

    [DisplayName("Data Final")]
    [Required]
    public DateTime DateTo { get; set; }

    [DisplayName("Tipos de Atividade")]
    public virtual List<ActivityType> ActivityTypeList { get; set; }

    [DisplayName("Atividades")]
    public virtual List<Activity> ActivityList { get; set; }

    [DisplayName("Clientes")]
    public virtual List<Client> ClientList { get; set; }

    public List<List<ActivityCounter>> ActivityCounterList { get; set; }

}
techspider
  • 3,370
  • 13
  • 37
  • 61
0
public ActivitiesReportViewModel()
{
    this.ActivityTypeList = new List<ActivityType>();
    this.ActivityList = new List<Activity>();
    this.ClientList = new List<Client>();
    this.ActivityCounterList = new List<List<ActivityCounter>>();
}

You are forgot to initialize your properties.

mihkov
  • 1,171
  • 13
  • 37
  • I know using always `this` keyword is better practice. Or maybe it's only my preference – mihkov May 09 '16 at 19:53
  • While this answer solves the problem, a good answer also includes explanation about what is wrong. That being said: this type of issue is so incredibly common that you shouldn't do all that and instead just flag the question as a duplicate of the canonical Q&A on the topic. – Jeroen Vannevel May 09 '16 at 19:53