1

I have a function called GetSuggestedAddresses and there's is a piece of it which calls a function that returns a List. I also have a property in my class that is type List. I tried using AddRange to add in my returned results to my property but it is throwing a null exception. I tried stepping through the code but I'm a bit lost. Any ideas? Here is my code-

 public List<ExpressAddressResult> GetSuggestedAddresses(string format = "xml")
  {
     foreach (RequestArrayRecord address in reqRecords) {
        string result = string.Empty;
        NameValueCollection collection = new NameValueCollection();
        collection.Add("id", AuthKey);
        collection.Add("line1", CleanUpAddress(address.AddressLine1));
        collection.Add("city", address.City);
        collection.Add("state", address.State);
        collection.Add("postalcode", address.Zip);
        collection.Add("maxrecords", MaxSuggestionResults.ToString());
        collection.Add("format", format);
        string parameters = GenerateParameters(collection);

        Uri serviceAddress = new Uri(RequestURL + parameters);
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(serviceAddress);
        using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) {
           if (response != null) {
              using (StreamReader streamReader = new StreamReader(response.GetResponseStream())) {
                 result = streamReader.ReadToEnd();
              }
           }
        }

        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.LoadXml(result);

        List<ExpressAddressResult> results = CreateListFromXml(xmlDoc);

        if (results != null) {
           AddressSuggestions.AddRange(results); // Throws Null Exception
        }
     }

     return AddressSuggestions; // This is a property of type List<ExpressAddressResult>
  }
Krazy Dev
  • 184
  • 3
  • 16
  • 1
    Where is `AddressSuggestions` defined? Have you verified that it is not null? – Richard Ev Jul 21 '16 at 15:44
  • 1
    Well, if I was to guess... `AddressSuggestions` is null. Have you stepped through this or inspected the variables when it breaks on the exception? – Charles Mager Jul 21 '16 at 15:44
  • 2
    Possible duplicate of [What is a NullReferenceException, and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Charles Mager Jul 21 '16 at 15:44
  • @RichardEverett AddressSuggestions is a property defined in my class. It is just public List AddressSuggestions {get; set;} – Krazy Dev Jul 21 '16 at 15:48
  • @CharlesMager Yes I've stepped through the code. Do I need to initialize my list before I add anything to it? I think that's where I might have messed up. – Krazy Dev Jul 21 '16 at 15:49
  • 1
    @KrazyDev yep, you need initialize it before using. – tym32167 Jul 21 '16 at 15:51
  • 1
    Yes. You should do it on your constructor to avoid checking null everywhere :) – Jorge Rojas Jul 21 '16 at 15:51

2 Answers2

2
var addressSuggestions = new List<ExpressAddressResult>();

Though I think you just want to return results.. It's been converted into the expected type. forget the addRange

Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
  • Yup! This was it! I forgot to initialize this in my constructor. Didn't realize that I had to initialize the List beforehand. – Krazy Dev Jul 21 '16 at 15:55
  • Cool glad you got it working, so AddressSuggestions is the list the drop down is binding to and it wasn't initialised. – Jeremy Thompson Jul 21 '16 at 16:00
1

The list to which you are actually adding the range is null, i.e. AddressSuggestions is null.

So make sure AddressSuggestions is initialized before you are adding any items to this list.

Yogi
  • 9,174
  • 2
  • 46
  • 61