1

可能重复:
.NET 中的 NullReferenceException 是什么?

我有一个与地址有关系的类事件,这是一个多对多的关系。当我尝试将地址添加到我的事件时,它会给出错误:

你调用的对象是空的。

这是我的代码:

事件类

  public class Event
  {
        public int EventID { get; set; }
        public string EventName { get; set; }
        public string EventImage { get; set; }
        public DateTime EventDate { get; set; }    
        public int ItemID { get; set; }
        public virtual Item Item { get; set; }

        public virtual ICollection<Address> Addresses { get; set; }
  }

地址等级

public class Address
{

    public int Address_id { get; set; }
    public string Address_street { get; set; }
    public int Address_housenumber { get; set; }
    public string Address_bus { get; set; }
    public Nullable<int> Messages_id { get; set; }

    public virtual ICollection<Event> Events { get; set; }
}

语境

  public class SocialGEOContext : DbContext
  {
    public DbSet<Event> Events { get; set; }
    public DbSet<Address> Addresses { get; set; }
    public DbSet<Item> Items { get; set; }

 protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {

        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();

      //EVENTS
        modelBuilder.Entity<Event>().HasKey(t => t.EventID);
        modelBuilder.Entity<Event>().ToTable("events", "geodatabase");
        modelBuilder.Entity<Event>().Property(t => t.EventID).HasColumnName("event_id");
        modelBuilder.Entity<Event>().Property(t => t.EventName).HasColumnName("event_name").IsRequired().IsUnicode(false).HasMaxLength(64);
        modelBuilder.Entity<Event>().Property(t => t.EventDate).HasColumnName("event_date");
        modelBuilder.Entity<Event>().Property(t => t.EventImage).HasColumnName("event_image").IsUnicode(false).HasMaxLength(128);
        modelBuilder.Entity<Event>().Property(t => t.ItemID).HasColumnName("item_id");
        modelBuilder.Entity<Event>().HasRequired(t => t.Item).WithMany(t => t.Events).HasForeignKey(d => d.ItemID);

//ADDRESSES
        modelBuilder.Entity<Address>().HasKey(t => t.Address_id);
        modelBuilder.Entity<Address>().ToTable("addresses", "geodatabase");
        modelBuilder.Entity<Address>().Property(t => t.Address_id).HasColumnName("address_id");
        modelBuilder.Entity<Address>().Property(t => t.Address_street).HasColumnName("address_street").IsRequired().IsUnicode(false).HasMaxLength(128);
        modelBuilder.Entity<Address>().Property(t => t.Address_housenumber).HasColumnName("address_housenumber");
        modelBuilder.Entity<Address>().Property(t => t.Address_bus).HasColumnName("address_bus").IsUnicode(false).IsFixedLength().HasMaxLength(1);
        //modelBuilder.Entity<Address>().Property(t => t.messages_id).HasColumnName("messages_id");
        //modelBuilder.Entity<Address>().HasOptional(t => t.message).WithMany(t => t.addresses).HasForeignKey(d => d.messages_id);
        modelBuilder.Entity<Address>()
            .HasMany(t => t.Events)
            .WithMany(t => t.Addresses)
            .Map(m =>
            {
                m.ToTable("event_has_address", "geodatabase");
                m.MapLeftKey("address_id");
                m.MapRightKey("event_id");
            });

事件控制器

        // POST: /Event/Create
        [HttpPost]
        public ActionResult Create(EventAddressViewModel ViewModel)
        {
            if (ModelState.IsValid)
            {
                Item viewItem = ViewModel.Item;
                Address viewAddress = ViewModel.Address;
                Event viewEvent = ViewModel.Event;

                //ADD ITEM
                viewItem.ItemCreateddate = DateTime.Now;
                db.Items.Add(viewItem);
                //ADD EVENT
                viewEvent.Item = viewItem;
                db.Addresses.Add(viewAddress);
                db.SaveChanges();
                Address addres1 = db.Addresses.Find(viewAddress.Address_id);
                viewEvent.Addresses.Add(addres1);
                db.Events.Add(viewEvent);

                db.SaveChanges();

                return RedirectToAction("Index");
            }
            return View(ViewModel);
        }

在我的控制器上加上“viewEvent.Addresses.Add(address);” 我得到了错误。当我将鼠标悬停在地址上时,它具有正确的值,但它一直说对象引用未设置...

4

2 回答 2

3

你没有实例化你的ICollection. 当您尝试添加地址时,您的Addresses变量为 null,这会导致NullReferenceException. 将其初始化为具体类型:

if(viewEvent.Addresses == null)
{
    viewEvent.Addresses = new List<Address>();
}
viewEvent.Addresses.Add(addres1);
于 2012-12-28T18:50:03.600 回答
1

将此行添加到失败的行上方:

viewEvent.Addresses = new List<Address>();

问题是您目前没有已初始化的地址列表,因此您需要先构建一个,然后再添加。

于 2012-12-28T18:49:39.420 回答