0

I have 2 simple webservice methods that query an XML document (stored in HTTP cache) that I am querying via javascript. The GetCitiesForAffiliate() method is throwing a NullReferenceException on the "select new" line when I pass in one perfectly valid item of data for the aff parameter. It works fine for other data in the aff parameter. The other method also works fine, even with the same aff parameter that causes the other method to bomb out.

I just validated the XML that I am querying. Both methods just return an empty json string when I pass in an aff that doesn't exist, which is OK. What should I look at that might be wrong?

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string GetCitiesForAffiliate(string aff)
    {
        LocationService loc = new LocationService();
        var query = (from center in loc.centersXml.Descendants("Center")
                     where center.Element("ServiceArea").Value.Equals(aff)
                     select new {
                         City = center.Element("City").Value
                     }).Distinct().OrderBy(x => x.City);

        JavaScriptSerializer serializer = new JavaScriptSerializer();
        string json = serializer.Serialize(query);

        return json;
    }

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string GetCentersForAffiliateCity(string aff, string city)
    {
        LocationService loc = new LocationService();
        var query = (from center in loc.centersXml.Descendants("Center")
                     where center.Element("ServiceArea").Value.Equals(aff) && center.Element("City").Value.Equals(city)
                     select new { 
                         ID = center.Element("ID").Value,
                         Name = center.Element("Name").Value 
                     }).Distinct().OrderBy(x => x.Name);

        JavaScriptSerializer serializer = new JavaScriptSerializer();
        string json = serializer.Serialize(query);

        return json;
    }
4
  • 1
    center.Element("City") could be null (meaning there is no City element). Commented Sep 25, 2012 at 21:53
  • OMG I found one that was blank just for "aff=26". FML Commented Sep 25, 2012 at 21:59
  • PS @DStanley I'll have to go read up on how to gracefully handle this error but if you have any links let me know. Thanks!! Commented Sep 25, 2012 at 22:38
  • I've found a topic on MSDN dealing with System.NullReferenceException. Maybe someone needs some advice on how to handle it. You'll find it here: blogs.msdn.microsoft.com/wriju/2011/05/04/… Commented Mar 25, 2016 at 14:33

1 Answer 1

5

If some of your nodes are missing a City element, you have some choices, depending on what output you want:

1) Add a check to see if the City element is null:

City = (center.Element("City") == null ? null : center.Element("City").Value)

2) Add where clause to ignore elements with a null city:

where center.Element("ServiceArea").Value.Equals(aff) 
    and center.Element("City") != null
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.