Hi could somebody please help me with this as it is driving me nuts for hours!!!
I keep receving the "NullReferenceException was unhandled " message:

public static Conditions GetCurrentConditions(string location)
            Conditions c;
            c = null;
            c = new Conditions();


            XmlDocument xmlConditions = new XmlDocument();
            xmlConditions.Load(string.Format("http://www.google.com/ig/api?weather={0}", location));
            if (xmlConditions.SelectSingleNode("xml_api_reply/weather/problem_cause") != null)
                c = null;
                Conditions cond = new Conditions();
                cond.Town = xmlConditions.SelectSingleNode("/xml_api_reply/weather/forecast_information/city").Attributes["data"].InnerText;
                cond.Condition = xmlConditions.SelectSingleNode("/xml_api_reply/weather/current_conditions/condition").Attributes["data"].InnerText;
                cond.TempC = xmlConditions.SelectSingleNode("/xml_api_reply/weather/current_conditions/temp_c").Attributes["data"].InnerText;
                cond.TempF = xmlConditions.SelectSingleNode("/xml_api_reply/weather/current_conditions/temp_f").Attributes["data"].InnerText;
                cond.Humidity = xmlConditions.SelectSingleNode("/xml_api_reply/weather/current_conditions/humidity").Attributes["data"].InnerText;
                cond.Wind = xmlConditions.SelectSingleNode("/xml_api_reply/weather/current_conditions/wind_condition").Attributes["data"].InnerText;


            return c;

Edited by __avd: Added [code] tags. For easy readability, always wrap programming code within posts in [code] (code blocks).

6 Years
Discussion Span
Last Post by mcriscolo

First, does the IDE tell you what line you're getting the NULL reference exception on?

Not sure how you want this to work, but when you check the XML node in your first "if" statement - if it's true (that is, the node is NOT null), you do this:

c = null;

Then exit the function, returning "c". If the caller of the function is not expecting the return value to be NULL, you have a problem. Also, if the value of the node is NULL, you go to the trouble of pulling various items out of the XML, assigning them to the local variable "cond" - but then you don't do anything with it.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.