Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm relatively new to .NET and MVC3. I'm having some trouble with the above error message when trying to add an instance of an object. Below is my code:

KeyDate class

public class KeyDate
{
    [Key]
    public int KeyID { get; set; }

    [StringLength(1000), Required]
    public string Title { get; set; }

    [Required]
    public string Description { get; set; }

    [Required]
    [Display(Name = "Event Date")]
    public DateTime EventDate { get; set; }

    [Display(Name = "Event End Date")]
    public DateTime? EventEndDate { get; set; }

    [Display(Name = "Course ID")]
    public int? CourseID { get; set; }

    [Display(Name = "Event ID")]
    public int? EventID { get; set; }

    public DateTime Created { get; set; }

    [Display(Name = "Created By")]
    public string CreatedBy { get; set; }

    public DateTime? Deleted { get; set; }

    [Display(Name = "Deleted By")]
    public string DeletedBy { get; set; }

    public virtual ICollection<Association> Associations { get; set; }
}

Association class

public class Association
{
    [Key, ForeignKey("KeyDate")]
    [Column(Order = 1)]
    public int KeyID { get; set; }

    [Key, ForeignKey("Category")]
    [Column(Order = 2)]
    public int CategoryID { get; set; }

    public virtual KeyDate KeyDate { get; set; }
    public virtual Category Category { get; set; }
}

I am getting the error in my Controller posted below

    [HttpPost]
    public ActionResult Create(KeyDate keyDate, int topic)
    {
        keyDate.Created = DateTime.Now;
        keyDate.CreatedBy = User.Identity.Name;

        // Create topic association
        Association keyDateTopic = new Association();
        keyDateTopic.CategoryID = topic;
        keyDate.Associations.Add(keyDateTopic); // <-- Object reference... error here

        repository.Add(keyDate);
        repository.Save();

        return RedirectToAction("Index");
    }

I have Googled this for ages, and have been following ScottGu's "Code First with Existing Database" example in which the code is quite similar, but I have had no luck. Any help appreciated. Thanks.

share|improve this question

4 Answers

up vote 2 down vote accepted

Your Assosiactions List needs to be instantiated before adding items to it.

Best practice is to do this from within the constructor.

public class KeyDate
{
...
    public virtual ICollection<Association> Associations { get; set; }
    public KeyDate()
    {
       Associations = new List<Association>()
    }
}
share|improve this answer
 
Awesome. It works. All part of the learning process... Thanks. –  Gareth Lewis Jan 5 '12 at 10:34

Associations is null. It has not been initialised.

Add a constructor for KeyDate and init it in there:

public KeyDate()
{
     Associations = new List<Association>();
}
share|improve this answer

Looks like keyDate.Associations is null.

Change

keyDate.Associations.Add(keyDateTopic);

to

if ( keyDate.Associations == null)
{
   keyDate.Associations = new Collection<Association>();
}

keyDate.Associations.Add(keyDateTopic);
share|improve this answer
 
Hi sll. When tracing through the code, I can see that keyDate.Associations is null. I'm not sure how I set this to an instance of an object. –  Gareth Lewis Jan 5 '12 at 10:27
 
If collection of associations is null you can manually cinstantiate it using keyDate.Associations = new Collection<Association>() –  sll Jan 5 '12 at 10:33

Description

It looks like keyDate.Associations is null

You have several ways to solve this

Sample

  • Create the ICollection in your ActionMethod.

    [HttpPost]
    public ActionResult Create(KeyDate keyDate, int topic)
    {
        keyDate.Created = DateTime.Now;
        keyDate.CreatedBy = User.Identity.Name;
    
        // Create topic association
        Association keyDateTopic = new Association();
        keyDateTopic.CategoryID = topic;
    
        // create this list
        keyDate.Associations = new List<Association>();
    
        keyDate.Associations.Add(keyDateTopic); // <-- Object reference... error here
    
        repository.Add(keyDate);
        repository.Save();
    
        return RedirectToAction("Index");
    }
    
  • OR Add a constructor to your KeyDate class like this

    public class KeyDate
    {
        public KeyDate()
        {
            // create this list
            this.Associations = new List<Association>();
        }
    
        [Key]
        public int KeyID { get; set; }
    
        [StringLength(1000), Required]
        public string Title { get; set; }
    
        [Required]
        public string Description { get; set; }
    
        [Required]
        [Display(Name = "Event Date")]
        public DateTime EventDate { get; set; }
    
        [Display(Name = "Event End Date")]
        public DateTime? EventEndDate { get; set; }
    
        [Display(Name = "Course ID")]
        public int? CourseID { get; set; }
    
        [Display(Name = "Event ID")]
        public int? EventID { get; set; }
    
        public DateTime Created { get; set; }
    
        [Display(Name = "Created By")]
        public string CreatedBy { get; set; }
    
        public DateTime? Deleted { get; set; }
    
        [Display(Name = "Deleted By")]
        public string DeletedBy { get; set; }
    
        public virtual ICollection<Association> Associations { get; set; }
    }
    
share|improve this answer
 
keyDate is not null since he already accessed it in the first line of the method body keyDate.Created = –  sll Jan 5 '12 at 10:28
 
@sll right! Thx –  dknaack Jan 5 '12 at 10:29

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.