user3258883 user3258883 - 1 month ago 9
C# Question

Error : Object reference not set to an instance of an object

I'm trying to add these lines to my SQL Server Database from Configuration.cs of Migration. The problem here is that, each time I try to execute this code from Package Manager Console using

Update-Database -Verbose -Force
I got this error :


Object reference not set to an instance of an object.


Here is what I try to execute

for (int i = 2014; i < 2018; i++)
{
AppMetierModel am = new AppMetierModel("Microsoft Visual Studio " + i, "C:\\Microsoft Visual Studio " + i, "...");

for (int j = 1; j < 4; j++)
{

FichierModel fm1 = new FichierModel("Fichier" + j, "Microsoft Visual Studio " + i, "fichier", "100.5 M", false, true, ".exe", am);
FichierModel dm = new FichierModel("Dossier" + j, "Microsoft Visual Studio " + i, "dossier", "", false, true, "", am);
FichierModel fm2 = new FichierModel("FichierSD" + j, "Microsoft Visual Studio " + i + "\\Dossier" + j, "fichier", "100.5 M", false, true, ".exe", am);
FichierModel fm3 = new FichierModel("FichierSD" + j + 1, "Microsoft Visual Studio " + i + "\\Dossier" + j, "fichier", "100.5 M", false, true, ".exe", am);
am.fichiers.Add(fm1);
am.fichiers.Add(fm2);
am.fichiers.Add(fm3);
am.fichiers.Add(dm);

context.AppsMetiers.AddOrUpdate(r => r.nomApplication,
am
);

context.Fichiers.AddOrUpdate(r => r.nomFichier,
fm1, fm2, fm3, dm
);
}
}


FichierModel :

public class FichierModel
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int id { get; set; } //auto-generated

[Required(ErrorMessage = "A remplir, champs obligatoire !")]
[Display(Name = "Nom")]
public string nomFichier { get; set; }

[Required(ErrorMessage = "A remplir, champs obligatoire !")]
[Display(Name = "Chemin")]
public string chemin { get; set; }

[Required(ErrorMessage = "A remplir, champs obligatoire !")]
[Display(Name = "Nature")]
public string nature { get; set; }

[Required(ErrorMessage = "A remplir, champs obligatoire !")]
[Display(Name = "Date ajout système")]
public DateTime dateAjoutSysteme { get; set; }

[Required(ErrorMessage = "A remplir, champs obligatoire !")]
[Display(Name = "Taille")]
public string taille { get; set; }

[Required(ErrorMessage = "A remplir, champs obligatoire !")]
[Display(Name = "Extension")]
public string extension { get; set; }

[Required(ErrorMessage = "A remplir, champs obligatoire !")]
[Display(Name = "Lecture seule ?")]
public bool lectureSeule { get; set; }

[Required(ErrorMessage = "A remplir, champs obligatoire !")]
[Display(Name = "Caché ?")]
public bool cache { get; set; }

[Required(ErrorMessage = "A remplir, champs obligatoire !")]
[Display(Name = "Application métier Mère")]
public AppMetierModel appMetier { get; set; }

[Display(Name = "Alertes liées")]
public ICollection<AlerteModel> alertes { get; set; }

public FichierModel() { }

public FichierModel(string nomFichier, string chemin, string nature, string taille, bool cache, bool lectureSeule, string extension, AppMetierModel appMetier)
{
this.nomFichier = nomFichier;
this.chemin = chemin;
this.nature = nature;
this.dateAjoutSysteme = DateTime.Now;
this.taille = taille;
this.lectureSeule = lectureSeule;
this.cache = cache;
this.extension = extension;
this.alertes = null;
this.appMetier = appMetier;
}
}


AppMetierModel :

public class AppMetierModel
{
[Key]
[Required(ErrorMessage = "A remplir, champs obligatoire !")]
[Display(Name = "Nom")]
public string nomApplication { get; set; }

[Required(ErrorMessage = "A remplir, champs obligatoire !")]
[Display(Name = "Date d'ajout système")]
public DateTime dateAjoutSysteme { get; set; }

[Required(ErrorMessage = "A remplir, champs obligatoire !")]
[Display(Name = "Chemin")]
public string chemin { get; set; }

[Required(ErrorMessage = "A remplir, champs obligatoire !"), StringLength(500, ErrorMessage = "Pas plus que 500 caractères.")]
[Display(Name = "Description")]
public string description { get; set; }

[Display(Name = "Liste des fichiers liées")]
public ICollection<FichierModel> fichiers { get; set; }

[Display(Name = "Liste des postes sur lesquels l'application est installée")]
public ICollection<PosteModel> postesInstalles { get; set; }

public AppMetierModel() { }

public AppMetierModel(string nomApplication, string chemin, string description)
{
this.nomApplication = nomApplication;
this.dateAjoutSysteme = DateTime.Now;
this.chemin = chemin;
this.description = description;
this.fichiers = null;
this.postesInstalles = null;
}
}


How do I fix this, please ?

Answer

AppMetierModel.fichiers is null, I don't see if it is initialized anywhere. Can't you pinpoint the exact location of the exception in Visual Studio's debugger?