locked
NullReferenceException RRS feed

  • Question

  • Hi, I am getting a NullReferenceException on validation of a Foreign Key.  The code causing the problem is below.  The error happens on a search data screen when trying to add a new record using the default AddAndEditNew modal.  Things to note:

    1. The NRE only happens 50% of the time.  Literally, if I run the application it works as intended.  If I stop debugging and then run it again, it throws the NRE.
    2. The FK (Vendor) is nullable.
    3. No user code on the screen/client, just default search data screen.
    4. No other code referencing the FK in the table code or data service.
    5. The excessive null checks were added just to make sure I wasn't missing an object or property somewhere in the chain.  I don't think all of them should be necessary for this to work.

    Your help is much appreciated!

    Code:

    Edit:

    I changed the code prior to the query to:

    If (Me.Vendor IsNot Nothing) Then
    Dim v As Nullable(Of Integer) = Me.Vendor.VendorID
    If (v.HasValue) AndAlso _
    (Not String.IsNullOrWhiteSpace(Me.FirstName)) AndAlso _
    (Not String.IsNullOrWhiteSpace(Me.LastName)) Then

    This seems to work a little better than before.  I still get the error 1/5 tries, so I still need some help understanding what is going on here.

    Thanks,

    • Edited by Hessc Saturday, February 9, 2013 7:49 PM
    Saturday, February 9, 2013 4:35 PM

Answers

  • As Yann already explained, within your query you are accessing f.Vendor.VendorID, so if f.Vendor is null, it will throw an exception. You should null check within the query, e.g.

    Where (f.Vendor IsNot Nothing) AndAlso (f.Vendor.VendorID == Me.Vendor.VendorID)
    You should also be able to tell Visual Studio to break when a NullReferenceException is thrown so that you can see exactly what line the exception comes from. See Simon Jones' first reply in this thread to see how to set this up.

    Justin Anderson, LightSwitch Development Team

    • Marked as answer by Hessc Monday, February 11, 2013 5:52 AM
    Monday, February 11, 2013 3:51 AM
    Moderator

All replies

  • I don't immediately see anything wrong with the second piece of code, but in the first one, you were checking Me.Vendor for not being null, but not f.Vendor. You then tried to use f.Vendor.VendorID, which would throw an NullReferenceException if f.Vendor was in fact null.

    Yann - LightSwitch Central - Click here for FREE Themes, Controls, Types and Commands
     
    Please click "Mark as Answer" if a reply answers your question. Please click "Vote as Helpful" , if you find a reply helpful.
     
    By doing this you'll help others to find answers faster.

    Sunday, February 10, 2013 1:51 PM
    Moderator
  • Yann, Thanks for your reply.  Wouldn't the line:

    If (result IsNot Nothing) ...

    check null on f.Vendor?  f.Vendor.VendorID is just part of the query Where clause, so if f.Vendor is null then the query result would be nothing, correct? 

    The values that used in the PropertyError result are all from Me.SomeObject.SomeProperty.

    Just want to make sure I am thinking about this the right way.  Thanks.


    • Edited by Hessc Monday, February 11, 2013 3:35 AM
    Monday, February 11, 2013 3:34 AM
  • As Yann already explained, within your query you are accessing f.Vendor.VendorID, so if f.Vendor is null, it will throw an exception. You should null check within the query, e.g.

    Where (f.Vendor IsNot Nothing) AndAlso (f.Vendor.VendorID == Me.Vendor.VendorID)
    You should also be able to tell Visual Studio to break when a NullReferenceException is thrown so that you can see exactly what line the exception comes from. See Simon Jones' first reply in this thread to see how to set this up.

    Justin Anderson, LightSwitch Development Team

    • Marked as answer by Hessc Monday, February 11, 2013 5:52 AM
    Monday, February 11, 2013 3:51 AM
    Moderator
  • Thanks Justin and Yann.
    Monday, February 11, 2013 5:53 AM
  • You're welcome.

    Yann - LightSwitch Central - Click here for FREE Themes, Controls, Types and Commands
     
    Please click "Mark as Answer" if a reply answers your question. Please click "Vote as Helpful" , if you find a reply helpful.
     
    By doing this you'll help others to find answers faster.

    Monday, February 11, 2013 8:49 AM
    Moderator
  • "Just want to make sure I am thinking about this the right way.  Thanks"

    I just want to address your comment, because no, you're not thinking about it correctly.

    "f.Vendor.VendorID is just part of the query Where clause, so if f.Vendor is null then the query result would be nothing, correct?"

    No, that's not correct. If anything in the where clause causes a null exception, code execution will stop, you won't just get "nothing" for the result.


    Yann - LightSwitch Central - Click here for FREE Themes, Controls, Types and Commands
     
    Please click "Mark as Answer" if a reply answers your question. Please click "Vote as Helpful" , if you find a reply helpful.
     
    By doing this you'll help others to find answers faster.

    Monday, February 11, 2013 9:35 AM
    Moderator
  • Thank Yann, I was definitely missing that important understanding.

    I think I am now having a different but related problem.  I used the debugging advice from Justin and it appears that the NRE is not coming from user code.  It is coming from the LS generated code (as far as I can tell).  I believe it could be because I am dereferencing some properties.  For example, I have code in a _Changed method that does this:

    If (UseVendorAddress = False) Then

    Me.Country = Nothing

    End If

    The boolean property UseVendorAddress is False by default and the debugger appears to be mad that Country is being set to Nothing.  Country is an FK.  Could that be the cause of an NRE, and if so, how to fix?

    As always, thanks!

    Monday, February 11, 2013 6:04 PM
  • You'd have to provide us the callstack for us to help you.

    Justin Anderson, LightSwitch Development Team

    Monday, February 11, 2013 10:15 PM
    Moderator
  • The only thing I can see in that code is I wonder if the relationship between Vendor & Country is 1-Many, or 0..1-Many.

    If it's 1-Many, you won't be able to set Me.Country to nothing, you'd have to retrieve an entity & set Me.Country to that entity.

    And just for clarity, so we're all using the same language, Country is not a FK, it's a navigation property. A FK is an integer property (usually) that is used in a relationship to join to the PK of another table.

    I hope that all helps.


    Yann - LightSwitch Central - Click here for FREE Themes, Controls, Types and Commands
     
    Please click "Mark as Answer" if a reply answers your question. Please click "Vote as Helpful" , if you find a reply helpful.
     
    By doing this you'll help others to find answers faster.

    Monday, February 11, 2013 11:51 PM
    Moderator
  • The relationship is 0..1-many.  I am going to work on this a bit more but will post a call stack if the exception continues.  Thanks for the clarification on the navigation property.
    Tuesday, February 12, 2013 12:11 AM