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.

4
  • What is a NullReferenceException and how do I fix it? Commented Jul 7, 2015 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. Commented Jul 7, 2015 at 10:30
  • Please post your dal.getAllVerifs() method. Commented Jul 7, 2015 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. Commented Jul 7, 2015 at 12:43

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.