1

I have a created this model;

namespace gantt.Models
{
    public class ganttModels
    {
        public IList<ganttModel> allGantt { get; set; }

    }

    public class ganttModel
    {
        public string projectName { get; set; }
        public IEnumerable<ResourcesSet> rescource { get; set; }
    }
}

Now my plan is to add items to this model, i have done this in a repository like this;

namespace gantt.Models
{
    public class GantDataRepository
    {
        GantEntities dbContext = new GantEntities();
        ganttModels returnModels = new ganttModels();
        ganttModel tempganttModel = new ganttModel();

        public GantDataRepository()
        {
            foreach (var item in dbContext.WorkPlans)
            {
                tempganttModel.projectName = item.Product;
                tempganttModel.rescource = item.ResourcesSets;
                returnModels.allGantt.Add(tempganttModel);   // Here i get the error     
            }   
        }

        public ganttModels getGant()
        {
            return returnModels;
        }    
    }
}

The repositor finds the data and add it. As i see it i have instansiate the returnModels already

5
  • returnModels.allGantt is never instantiated... Commented Apr 18, 2013 at 11:21
  • but dont it fall under "returnModels"? Do i have to instansiate all the underlaying variables in a class? Commented Apr 18, 2013 at 11:22
  • @Tim Yes you do. The default value for reference types is null. Interfaces, as well as classes and delegates, are declared as reference types. Commented Apr 18, 2013 at 11:28
  • have you really try yourself ? Commented Apr 18, 2013 at 11:35
  • Arun, for 2-3 hours, im sorry but im pretty new to this world and all i could find on the matter was to instansiate it.And i do admit im abit ashamed. Commented Apr 18, 2013 at 11:37

6 Answers 6

9

returnModels is initialized.

returnModels.allGantt is not.

You can do this:

public class ganttModels
{
    public IList<ganttModel> allGantt { get; set; }
    public ganttModels()
    {
        allGantt = new List<ganttModel>();
    }
}

Or something.

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

1 Comment

Ah, i see.. Sorry... This was really stupid of me, i stared myself blind on the main datatype :/
1

You are not instantiating your list inside ganttModels Class.

public class ganttModels
{
    public ganttModels(){
      allGantt = new List<ganttModel>();      
    }

    public IList<ganttModel> allGantt { get; set; }

}

Comments

1

returnModels.allGantt in null when you try to add to it.

Either creat an instance of List in the constructor of gantModels or

prior to the Add call

returnModels.allGantt = new List<gantModel>();

Comments

1

You can either define a constructor as Conrad has it in the answer, or you can get rid of auto implemented property and do:

private IList<ganttModel> _allGantt = new List<ganttModel>();

public IList<ganttModel> AllGantt
{
    get { return _allGantt; }
    set { _allGantt = value; }
}

Comments

1

You need to define your ganttModels class as follows:

public class GanttModels // use correct casing!
{
    public GanttModels() { this.AllGantt = new List<GanttMode>(); }
    public IList<GanttModel> AllGantt { get; private set; }
}

You are also re-using the same reference in your repository so you should do this instead:

public class GantDataRepository
{
    GantEntities dbContext = new GantEntities();
    GanttModels returnModels = new GanttModels();

    public GantDataRepository()
    {
        foreach (var item in dbContext.WorkPlans)
        {
            returnModels.AllGantt.Add(new GanttModel 
            {
                ProjectName = item.Product,
                Rescource = item.ResourcesSets
            });
        }
    }

Comments

0

you need to initilize allGantt....

    private IList<ganttModel> _allGantt;
    public IList<ganttModel> allGantt { 
          get{ return _allGantt ?? (_allGantt = new new List<ganttModel>()); } 
          set{ _allGantt = value;} 
     }

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.