none
How to Avoid Null Dereferences RRS feed

  • Question

  • I may have posted this question a couple days ago in an archived forum?  So I'm trying this forum

    Hi All,

    How do you check for nulls within LINQ? 

    fieldConfig.Where(k => (k.DataValue as DatabaseField).Name == assnAttr.ThisKey).ToList().ForEach(k => (k.DataValue as DatabaseField).ForeignKey = true);

    For example, Fortify software flags Null Dereferences saying there's something that could me null.  K could be null or (K.DataValue as DatabaseField) could be null.  But I can't check for K outside of this where statement because it doesn't make sense, it's not in scope. Is it possible to check for nulls?

    (k.DataValue as DatabaseField)

    Thank you,

    Nici

    Thursday, April 25, 2013 4:26 PM

Answers

  • Hi Nici;

    Try it like this and see what you get.

    fieldConfig.Where(k => k != null && k.DataValue != null &&  (k.DataValue as DatabaseField).Name == assnAttr.ThisKey).ToList().ForEach(k => (k.DataValue as DatabaseField).ForeignKey = true);

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    • Marked as answer by KonaCoffee Friday, April 26, 2013 2:40 PM
    Thursday, April 25, 2013 5:15 PM

All replies

  • Hi Nici;

    Try it like this and see what you get.

    fieldConfig.Where(k => k != null && k.DataValue != null &&  (k.DataValue as DatabaseField).Name == assnAttr.ThisKey).ToList().ForEach(k => (k.DataValue as DatabaseField).ForeignKey = true);

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    • Marked as answer by KonaCoffee Friday, April 26, 2013 2:40 PM
    Thursday, April 25, 2013 5:15 PM
  • Thank you, Fernando!  Fortify software still flags it was an issue but that might be enough ammo to argue some of these.

    Thank you,

    Nici

    Friday, April 26, 2013 2:40 PM
  •   

    Not a problem Nici, glad I was able to be of some assistance.

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Friday, April 26, 2013 3:04 PM