ASP.NET MVC3 - “Object reference not set to an instance of an object” error

帅比萌擦擦* 提交于 2020-01-02 08:44:34

问题


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.


回答1:


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>()
    }
}



回答2:


Associations is null. It has not been initialised.

Add a constructor for KeyDate and init it in there:

public KeyDate()
{
     Associations = new List<Association>();
}



回答3:


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);



回答4:


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; }
    }
    


来源:https://stackoverflow.com/questions/8740877/asp-net-mvc3-object-reference-not-set-to-an-instance-of-an-object-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!