Question OutOfRangeExceptionHandler error while debugging

raffs03

Member
Joined
Jan 18, 2012
Messages
6
Programming Experience
Beginner
Hello everyone i need some help regarding to my problem.

I'm making a weatherforecast apllication(REST method in C# language)
i write the code and it shows no error or warnings.
but when i run the debugger it show an error(See pics)
PS: im using Visual basic 2010 professional edition.

http://imageshack.us/photo/my-images/844/error1z.jpg/

This is the codes that i write.

using System.Xml.Linq;
using System.Net;




namespace RESTService
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();


            // Obtain the Foracast.
            GetForecast();


            // Display the Forecast data.
            DisplayData(ForecastNumber);
        }
        // Holds all of the weather data
        XDocument GetData;


        // Contains just the forecast.
        IEnumerable<XElement> Forecast;


        //Specifies which Forecast to use.
        Int32 ForecastNumber;


        // Defines the maximum number of farecast.
        Int32 MaxForecasts;


        // Contains a list of icons associated with the forecast.
        IEnumerable<XElement> Icons;


        // Specifies which icon to use.
        Int32 IconNumber;


public void GetForecast()
{
    // Contains the result of a retry request.
    DialogResult TryAgain = DialogResult.Yes;


    // Keep trying to get the weather data until the user
    // Gives up or the call is successful.
    while (TryAgain == DialogResult.Yes)
    {
        try
        {
            // Get the weather data.
            GetData =
                XDocument.Load(
                "http://api.wunderground.com/auto/wui/geo" 
                + "/ForecastXML/index.xml?query="
                + txtLocation.Text);


            // End the loop
            TryAgain = DialogResult.No;
        }
        catch (WebException)
        {
            TryAgain = MessageBox.Show(
                "Couldn't obtain the weather data! \r\n Try again?",
                "Data Download Error",
                MessageBoxButtons.YesNo,
                MessageBoxIcon.Error);
        }
    }


    // Obtain all the forecasts.
    Forecast =
        GetData.Element("forecast").Element("txt_forecast").Elements("forecastday");


    // Define the maximum number of forecasts.
    MaxForecasts = Forecast.Count() - 1;


    // Specify which forecast to use.
    ForecastNumber = 1;


    // Specify which icon to use.
    IconNumber = 1;


    // Reset the buttons.
    btnNext.Enabled = true;
    btnPrevious.Enabled = false;
}


public void DisplayData(Int32 FNumber)
{
    // Display the title of the current forecast.
    txtTitle.Text = Forecast.ElementAt(FNumber).Element("title").Value;


    // Display detailed forecast information.
    txtForecast.Text = Forecast.ElementAt(FNumber).Element("fctext").Value;


    // Obtain a list of icons associated with the forecast.
    Icons = Forecast.ElementAt(FNumber).Element("icons").Elements("icon_set");


    // Define the maximum numbers of available icons.
    IconSelect.Maximum = Icons.Count() - 1;


    // Display the icon on screen.
    wbIcon.Url = new Uri(Icons.ElementAt(IconNumber).Element("Icon_url").Value);
}



Can somebody correct what error i made? Because i try fixing it almost an hour but i can't see my error
i tried changing the value of "Fnumber" but the error is still there.
PS: I hope this is the right place to post this topic.
 
Last edited by a moderator:
txtTitle.Text = Forecast.ElementAt(FNumber).Element("title").Value;
According to exception there is not an element at index FNumber.
 
Back
Top Bottom