简体   繁体   中英

C# Mvc Entity Framework Nullpointer Exception at List.Add

When I try to Entity Framework with Mvc. I want to insert some data when database creating but Fword class has tree properties.When I try to add to its List or List I got NullPointerException at Seed method. Could anyone tell me where I go wrong?

Here are DataAccess Classes

public class MyContext : DbContext
{ 
    public MyContext () : base("MyContext")
    {
        Database.SetInitializer(new VeritabaniOlusturucu());
    }


    public DbSet<FWord> words { get; set; }
    public DbSet<WordType> WT { get; set; }
    public DbSet<WordFrequency> WF { get; set; }

  }

At Below classes seed method I get the error.

public class VeritabaniOlusturucu : CreateDatabaseIfNotExists<EWLContext>
{
    protected override void Seed(MyContext  context)
    {
        WType wt = new WType();
        wt.Type = "determiner";
        FWord fw = new FWord();
        fw.Word = "try";
        fw.WT.Add(wt);//I got here error
        fw.WF.Add(new WordFrequency { Frequency = "B1" });//and here
        fw.WF.Add(new WordFrequency { Frequency = "B2" });//and here
        context.FWords.Add(fw);
        context.SaveChanges();
    }

}

Here are Entity Classes

[Serializable]
public class FWord
{
    public FWord()
    {

    }
    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }
    public String Word { get; set; }
    public List<WordType> WT { get; set; }
    public List<WordFrequency> WF { get; set; }
}
[Serializable]
public class WordType
{
    public WordType()
    {

    }
    [Key]
    public int Id { get; set; }
    public String Type { get; set; }

    public virtual FWord Freq { get; set; }
}
[Serializable]
public class WordFrequency
{
    public WordFrequency()
    {

    }
    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }
    public String Frequency { get; set; }
    public virtual FWord Freq { get; set; }
 }

Thank you.

Your list is null. You have to instanciate it before adding items into it.

protected override void Seed(MyContext  context)
    {
        WType wt = new WType();
        wt.Type = "determiner";
        FWord fw = new FWord();
        fw.Word = "try";
        fw.WT = new List<WordType>();
        fw.WT.Add(wt);
        fw.WF.Add(new WordFrequency { Frequency = "B1" });//and here
        fw.WF.Add(new WordFrequency { Frequency = "B2" });//and here
        context.FWords.Add(fw);
        context.SaveChanges();
    }

Your list is a property which is not initialized and unless you set it, it is null. if you want to avoid this even if it is not set, give it a default value:

public List<WordType> WT { get; set; } = new List<WordType>()
public List<WordFrequency> WF { get; set; } = new List<WordFrequency>();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM