0

I am getting this error when I try to retrieve some data. Here is how I am querying

   using (_realtyContext = new realtydbEntities())
   {
                foreach (int yr in years)
                {
                    var standard = (from feest in _realtyContext.FeeStandards
                                    where feest.Year == yr && feest.FeeCategory.CategoryName == "PropertyFee"
                                    select feest).SingleOrDefault();

                    var chargeItem = (from chrgItem in _realtyContext.PropertyFees
                                      where chrgItem.FeeStandardID == standard.FeeStandardID
                                      && chrgItem.HousholdId == Householder.HouseholdID
                                      select chrgItem).SingleOrDefault();

                        this._propertyFees.Add(chargeItem);
                        this._standards.Add(standard);
                }
   }

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

the error is on the 2nd query.(var chargeItem)

this query throw an error: var chargeItem =...

2
  • 2
    Which line is causing the error? Commented May 3, 2012 at 13:37
  • 1
    Please give your question more meaningful topic. Commented May 3, 2012 at 13:39

2 Answers 2

3

.SingleOrDefault() returns null when no records are found, and in the next statement you're using "standard" as if it is never null.

But that's just one of the many possible causes...

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

Comments

1

you have to check if standard is null. SingleOrDefault() will return null if there are no results

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.