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?
    – glosrob
    May 3, 2012 at 13:37
  • 1
    Please give your question more meaningful topic. 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...

1

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

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.