0

I have this piece of code :

 Verification verif = dal.getAllVerifs().Where(v => v.interfa == inter).ToList().FirstOrDefault(v => v.nom == tache.nom);
 string name = verif.str.nomStruct;
 return RedirectToAction("Index", "Home", new {error = name });

An exception is thrown :

Object reference not set to an instance of an object.

I looked it up, my Verification object has every attribute filled, except for the "str" attribute ( a "Structure" object), which is null .

This is just supposed to return all the rows of one of my tables :

public List<Verification> getAllVerifs()
{
  return bdd.verifications.ToList();
}

my model :

[Table("Structure")]
 public class Structure
 {
     [Key]
     [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
     public int strucutureId { get; set; }
     [Required]
     public string nomStruct { get; set; }
     [Required]
     public bool isXsdExistant { get; set; }
 }

[Table("Taches_Verification")]
public class Verification
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int VerifId { get; set; }
    [Required]
    public string nom { get; set; }
    [Required]
    public string feuille { get; set; }
    [Required]
    public Interface interfa { get; set; }
    [Required]
    public Structure str { get; set; }
    [Required]
    public int numOrdre { get; set; }

}

I looked in the database, the primary key of the table that contains the "structure" objects is filled and is ok.

Have you ever experienced this problem ?

Thanks

K.

falsarella
  • 12,217
  • 9
  • 69
  • 115
Kypaz
  • 411
  • 3
  • 11
  • [What is a `NullReferenceException` and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Soner Gönül Jul 07 '15 at 10:18
  • You're asking the wrong question so people start marking as duplicate of this pet question. The real question is, why is `verif.str` null? EF doesn't support structs, that's all. – Gert Arnold Jul 07 '15 at 10:30
  • Please post your `dal.getAllVerifs()` method. – Jurgen Camilleri Jul 07 '15 at 10:50
  • OK, `Struture` is no struct. Well, `public Structure str` isn't virtual, so it doesn't load lazily. And/or in `getAllVerifs` you don't use `Include`. – Gert Arnold Jul 07 '15 at 12:43

1 Answers1

2

Writing everything in one line is great untill something does not work.

Break your code up into pieces and debug it.

var verif = dal.getAllVerifs()
var veriflimited = verif.Where(v => v.interfa == inter).ToList()
var singleVerif =veriflimited.FirstOrDefault(v => v.nom == tache.nom);
string name = verif.str.nomStruct;

If you run that you will most likely find out which of the objects you are trying to manipulate that is null and then it will be easier to see why.

Also you are doing things that could be done in one step in many..

This will probably show the same result and run faster.

dal.getAllVerifs().FirstOrDefault(x => 
    v.interfa == inter 
    && v.nom == tache.nom
);

Also, what are you doing in .getAllVerifs()?

JensB
  • 6,663
  • 2
  • 55
  • 94