NullReferenceException was unhandled

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tookerello
    New Member
    • May 2010
    • 5

    NullReferenceException was unhandled

    Hi could you please help with this as i have been trying to correct it for hours:
    I keep receiving the "NullReferenceE xception was unhandled" message

    Code:
    public static Conditions GetCurrentConditions(string location)
            {
                Conditions c;
                c = null;
                c = new Conditions();
    
                c.GetCurrentConditions();
    
                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;
                }
                else
                {
                    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;
            }
    Last edited by tlhintoq; May 5 '10, 03:57 PM. Reason: [CODE] ...Your code goes between code tags [/CODE]
  • Monomachus
    Recognized Expert New Member
    • Apr 2008
    • 127

    #2
    Originally posted by tookerello
    Hi could you please help with this as i have been trying to correct it for hours:
    I keep receiving the "NullReferenceE xception was unhandled" message

    public static Conditions GetCurrentCondi tions(string location)
    {
    Conditions c;
    c = null;
    c = new Conditions();

    c.GetCurrentCon ditions();

    XmlDocument xmlConditions = new XmlDocument();
    xmlConditions.L oad(string.Form at("http://www.google.com/ig/api?weather={0} ", location));
    if (xmlConditions. SelectSingleNod e("xml_api_repl y/weather/problem_cause") != null)
    {
    c = null;
    }
    else
    {
    Conditions cond = new Conditions();
    cond.Town = xmlConditions.S electSingleNode ("/xml_api_reply/weather/forecast_inform ation/city").Attribut es["data"].InnerText;
    cond.Condition = xmlConditions.S electSingleNode ("/xml_api_reply/weather/current_conditi ons/condition").Att ributes["data"].InnerText;
    cond.TempC = xmlConditions.S electSingleNode ("/xml_api_reply/weather/current_conditi ons/temp_c").Attrib utes["data"].InnerText;
    cond.TempF = xmlConditions.S electSingleNode ("/xml_api_reply/weather/current_conditi ons/temp_f").Attrib utes["data"].InnerText;
    cond.Humidity = xmlConditions.S electSingleNode ("/xml_api_reply/weather/current_conditi ons/humidity").Attr ibutes["data"].InnerText;
    cond.Wind = xmlConditions.S electSingleNode ("/xml_api_reply/weather/current_conditi ons/wind_condition" ).Attributes["data"].InnerText;

    }

    return c;
    }
    put a try-catch block into your method GetCurrentCondi tions and see where exactly it throws exception and tell us more details.

    Comment

    • tlhintoq
      Recognized Expert Specialist
      • Mar 2008
      • 3532

      #3
      TIP: When you first created your question you were asked to wrap your code with [code] tags.
      [imgnothumb]http://files.me.com/tlhintoq/10jihf[/imgnothumb]
      It really does help a bunch. Look how much easier it is to read now that someone has done it for you. Its the button with a '#' on it. [imgnothumb]http://img689.imagesha ck.us/img689/2685/codebuttonbytes com.png[/imgnothumb] More on tags. They're cool. Check'em out.

      Comment

      • tlhintoq
        Recognized Expert Specialist
        • Mar 2008
        • 3532

        #4
        Originally posted by OriginalPoster
        Original Poster: "How do I fix a 'object reference not set to an instance of an object' error?
        The line your code stopped on while debugging holds all your answers.
        One of the variables/objects was created but not initialized. For example:
        Code:
        string TempString;// Created but not initialized so this is still null
        //versus
        string TempString = string.empty;
        Debug your project again. This time, when it breaks on a line look at the Locals pallet to see which variable/object is null. You can also hover your mouse over each variable and the hothelp will shows its value. One of them will be null.

        Comment

        • tlhintoq
          Recognized Expert Specialist
          • Mar 2008
          • 3532

          #5
          Code:
          Conditions c;
                      c = null;
                      c = new Conditions();
          can be reduced to
          Code:
          Conditions c = new Conditions();
          Why have 3 lines when 1 will do just as cleanly?

          Comment

          Working...