1

My intention is to develop a C# form application that retrieves weather data from yahoo weather api. I need the program to get weather data from yahoo and retrieves in the respective text input. The code is as follows..

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Linq;
using System.Xml;
using System.IO;
using System.Web;
using System.Speech.Recognition;
using System.Speech.Synthesis;

namespace weather
{
    public partial class Form1 : Form
    {
        string Temperature;
        string Condition;
        string Humidity;
        string WindSpeed;
        string Town;
        string TFCond;
        string TFHigh;
        string TFLow;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void GetWeather()
        {
            string query = String.Format("http://weather.yahooapis.com/forecastrss?w=1319153");
            //string query = String.Format("http://weather.yahooapis.com/forecastrss?w=2502265");
            XmlDocument wData = new XmlDocument();
            wData.Load(query);

            XmlNamespaceManager manager = new XmlNamespaceManager(wData.NameTable);
            manager.AddNamespace("yweather","http://xml.weather.yahoo.com/ns/rss/1.0");

            XmlNode channel = wData.SelectSingleNode("rss").SelectSingleNode("channel");
            XmlNodeList nodes = wData.SelectNodes("/rss/channel/item/yweather:forecast", manager);

            Temperature = channel.SelectSingleNode("item").SelectSingleNode("yweather:condition", manager).Attributes["temp"].Value;

            Condition = channel.SelectSingleNode("item").SelectSingleNode("yweather:condition", manager).Attributes["text"].Value;

            Humidity = channel.SelectSingleNode("yweather:atmosphere", manager).Attributes["humidity"].Value;

            WindSpeed = channel.SelectSingleNode("yweather:wind", manager).Attributes["speed"].Value;

            Town = channel.SelectSingleNode("yweather:location", manager).Attributes["city"].Value;

            TFCond = channel.SelectSingleNode("item").SelectSingleNode("yweather:forecast", manager).Attributes["text"].Value;

            TFHigh = channel.SelectSingleNode("item").SelectSingleNode("yweather:forecast", manager).Attributes["high"].Value;

            TFLow = channel.SelectSingleNode("item").SelectSingleNode("yweather:forecast", manager).Attributes["low"].Value; 
        }

        private void button1_Click(object sender, EventArgs e)
        {
            textBox1.AppendText(Temperature);
            textBox2.AppendText(Humidity);
        }

    }
}

The gui is as follows...

enter image description here

I need the help of kind programmers.

senye
  • 175
  • 2
  • 15
  • When you run this in the debugger, on what line is the `NullReferenceException` thrown? – Yuck Dec 14 '14 at 20:57
  • Read the exception: `textBox1` and/or `textBox2`are `null`. Could also be `Temperature` or `Humidity` are `null` - but that's less likely. – John3136 Dec 14 '14 at 20:58
  • @John3136 Reread this code and tell me again that Temperature or Humidity are "less likely" nulls. – MajkeloDev Dec 14 '14 at 21:01
  • It's damn sure They're nulls. – MajkeloDev Dec 14 '14 at 21:02
  • @MajkeloDev I didn't read the whole code - I can tell by the exception that one of those 4 things is null. In my experience it's normally the control itself that is null, but that's not always right - like in this case. (If I was writing the API I'd handle a null arg as a NOP) – John3136 Dec 14 '14 at 21:05
  • There is nothing bad about this API. API is all right OP just havn't used it actually :) – MajkeloDev Dec 14 '14 at 21:26

1 Answers1

4

You defined Variables Temperature nad Humidity but You never assign them so they are actually just nulls. You need to use GetWeather() method. You just defined this method but never used it so Your Temperatura and Humidity variables are still nulls.

In Your EventHandler for button click You need to use GetWeather method.

private void button1_Click(object sender, EventArgs e)
    {
        GetWeather();
        textBox1.AppendText(Temperature);
        textBox2.AppendText(Humidity);
    }
MajkeloDev
  • 1,661
  • 13
  • 30
  • thank you for ur support.. I am sorry for asking this kind of silly question here. – senye Dec 14 '14 at 21:07
  • Thanks a lot. Maybe you need to translate Fahrenite to celsius (F-32)/100 Also you nee an array of weather condition code list. Old one of the list is here https://gist.github.com/bzerangue/805520 – matasoy Mar 22 '16 at 20:11