0

This code, located below, is designed to pull an XML file from the internet and parse it below. However, an exception (NullReferanceExeption) is caused when I attempt to extract a value from this XML file.

Here's the code, the line has been noted where the error is occurring:

Below the code is the XML file I am trying to extract the value from.

//Create the forecast
void makeforecast()
{
    //Make Webclient Request
    WebClient client = new WebClient();
    client.DownloadStringAsync(new Uri("http://api.wt360.com/data/forecast/daily-4day?key=ic1uji3s9zjnjgcd6hq64ny6alkoin9119vjrxc1wwngdzuihk&l=" + latitude + "," + longitude + "&fmt=xml"));
    client.DownloadStringCompleted += (s, n) =>
    {
        progress.Visibility = System.Windows.Visibility.Collapsed;
        ((Storyboard)this.Resources["trans"]).Begin();
        XDocument xDoc = XDocument.Parse(n.Result);

        //Current Conditions
        if (debug2.Text == "Temp")
        {
        }
        if (debug2.Text == "Sun")
        {
        }
        if (debug2.Text == "Rain")
        {
           THIS LINE CAUSES THE EXEPTION >> tempnow.Text = xDoc.Root.Element("forecast").Element("pop").Value;**
        }
    }
}

And the XML:

<data>
<status>success</status>
<location>
<city/>
<fullName>39.6N, 0.3W - 6miles SW of El Puerto, SP</fullName>
<localDate>2014-01-30T01:29:23</localDate>
<localEpoch>1391045363</localEpoch>
<locationID>GP202368</locationID>
</location>
<forecast>
<record>0</record>
<utcEpoch>1391040000</utcEpoch>
<utcDate>2014-01-30T00:00:00</utcDate>
<maxTempC>15.4</maxTempC>
<maxTempF>59.7</maxTempF>
<minTempC>5.2</minTempC>
<minTempF>41.3</minTempF>
<fcstDay>1</fcstDay>
<dayLength>10:07</dayLength>
<sunriseHHMMLocal>8:11am</sunriseHHMMLocal>
<sunsetHHMMLocal>6:18pm</sunsetHHMMLocal>
<nightLength/>
<moonriseHHMMLocal/>
<moonsetHHMMLocal/>
<moonphase/>
<moonillum/>
<moonicon/>
<pop>19</pop>
<uv_index>2</uv_index>
<gustC>30</gustC>
<gustF>19</gustF>
<heatindexC>-999.99</heatindexC>
<heatindexF>-999.99</heatindexF>
<icon>partly-cloudy-sm.png</icon>
<iconBase>partly-cloudy</iconBase>
<iconLg>partly-cloudy.png</iconLg>
<prcpC>0</prcpC>
<prcpF>0</prcpF>
<rh>33</rh>
<skyCover>8</skyCover>
<snowC>0</snowC>
<snowF>0</snowF>
<wdir>NW</wdir>
<windchillC>-999.99</windchillC>
<windchillF>-999.99</windchillF>
<wspdC>16</wspdC>
<wspdF>10</wspdF>
<wx>Mostly Sunny</wx>
</forecast>
<forecast>
<record>1</record>
<utcEpoch>1391126400</utcEpoch>
<utcDate>2014-01-31T00:00:00</utcDate>
<maxTempC>16</maxTempC>
<maxTempF>60.8</maxTempF>
<minTempC>5</minTempC>
<minTempF>40.9</minTempF>
<fcstDay>2</fcstDay>
<dayLength>10:09</dayLength>
<sunriseHHMMLocal>8:10am</sunriseHHMMLocal>
<sunsetHHMMLocal>6:19pm</sunsetHHMMLocal>
<nightLength/>
<moonriseHHMMLocal/>
<moonsetHHMMLocal/>
<moonphase/>
<moonillum/>
<moonicon/>
<pop>18</pop>
<uv_index>2</uv_index>
<gustC>30</gustC>
<gustF>19</gustF>
<heatindexC>-999.99</heatindexC>
<heatindexF>-999.99</heatindexF>
<icon>partly-cloudy-sm.png</icon>
<iconBase>partly-cloudy</iconBase>
<iconLg>partly-cloudy.png</iconLg>
<prcpC>0</prcpC>
<prcpF>0</prcpF>
<rh>46</rh>
<skyCover>28</skyCover>
<snowC>0</snowC>
<snowF>0</snowF>
<wdir>WNW</wdir>
<windchillC>-999.99</windchillC>
<windchillF>-999.99</windchillF>
<wspdC>13</wspdC>
<wspdF>8</wspdF>
<wx>Mostly Sunny</wx>
</forecast>
<forecast>
<record>2</record>
<utcEpoch>1391212800</utcEpoch>
<utcDate>2014-02-01T00:00:00</utcDate>
<maxTempC>18.1</maxTempC>
<maxTempF>64.5</maxTempF>
<minTempC>6.4</minTempC>
<minTempF>43.5</minTempF>
<fcstDay>3</fcstDay>
<dayLength>10:12</dayLength>
<sunriseHHMMLocal>8:09am</sunriseHHMMLocal>
<sunsetHHMMLocal>6:21pm</sunsetHHMMLocal>
<nightLength/>
<moonriseHHMMLocal/>
<moonsetHHMMLocal/>
<moonphase/>
<moonillum/>
<moonicon/>
<pop>46</pop>
<uv_index>2</uv_index>
<gustC>49</gustC>
<gustF>31</gustF>
<heatindexC>-999.99</heatindexC>
<heatindexF>-999.99</heatindexF>
<icon>light-rain-shower-sm.png</icon>
<iconBase>light-rain-shower</iconBase>
<iconLg>light-rain-shower.png</iconLg>
<prcpC>0.2</prcpC>
<prcpF>0.09</prcpF>
<rh>35</rh>
<skyCover>27</skyCover>
<snowC>0</snowC>
<snowF>0</snowF>
<wdir>WNW</wdir>
<windchillC>-999.99</windchillC>
<windchillF>-999.99</windchillF>
<wspdC>22</wspdC>
<wspdF>14</wspdF>
<wx>Light Rain Showers</wx>
</forecast>
<forecast>
<record>3</record>
<utcEpoch>1391299200</utcEpoch>
<utcDate>2014-02-02T00:00:00</utcDate>
<maxTempC>15.9</maxTempC>
<maxTempF>60.7</maxTempF>
<minTempC>4.2</minTempC>
<minTempF>39.6</minTempF>
<fcstDay>4</fcstDay>
<dayLength>10:14</dayLength>
<sunriseHHMMLocal>8:08am</sunriseHHMMLocal>
<sunsetHHMMLocal>6:22pm</sunsetHHMMLocal>
<nightLength/>
<moonriseHHMMLocal/>
<moonsetHHMMLocal/>
<moonphase/>
<moonillum/>
<moonicon/>
<pop>12</pop>
<uv_index>2</uv_index>
<gustC>41</gustC>
<gustF>26</gustF>
<heatindexC>-999.99</heatindexC>
<heatindexF>-999.99</heatindexF>
<icon>sunny-sm.png</icon>
<iconBase>sunny</iconBase>
<iconLg>sunny.png</iconLg>
<prcpC>0</prcpC>
<prcpF>0</prcpF>
<rh>34</rh>
<skyCover>0</skyCover>
<snowC>0</snowC>
<snowF>0</snowF>
<wdir>WNW</wdir>
<windchillC>-999.99</windchillC>
<windchillF>-999.99</windchillF>
<wspdC>19</wspdC>
<wspdF>12</wspdF>
<wx>Sunny</wx>
</forecast>
</data>

Please do not link this as a duplicate of NullReferenceException with XML Document as major changes have been made to the code.

Community
  • 1
  • 1
Kevin
  • 383
  • 2
  • 11
  • 2
    The code you've given works with the precise XML you've shown us here. You should do debugging to check exactly what's causing the NullReferenceException - is `tempnow` null? Is `xDoc.Element("forecast")` null? Is `xDoc.Element("forecast").Element("pop")` null? – Jon Skeet Jan 30 '14 at 03:20
  • That's what is so strange here, none of them appear to be null. – Kevin Jan 30 '14 at 03:31
  • I find that hard to believe, unless you're really not running the code you think you are. (It's really unclear why you're using that `debug2.Text` part at all... that seems to be unrelated, so I'd strip it out.) I suggest you try writing a console app which loads the XML directly with `XDocument.Load` (passing in the URL) and try to reproduce the problem that way. Then we can check it for ourselves very easily. – Jon Skeet Jan 30 '14 at 03:32
  • That must be the case. From what I know, the value of pop should be 19, and forecast is an element leading to pop. There are multiple "forecast" elements in XML, but i'm almost certain it just picks out the first instance of it if I don't specify otherwise. I'll try the console app thing, and the debug2.text is related, you just can't see the code it's related to. – Kevin Jan 30 '14 at 03:39
  • Almost all cases of `NullReferenceException` are the same. Please see "[What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)" for some hints. – John Saunders Jan 30 '14 at 03:44

2 Answers2

0

You don't seem to be checking that the XML is as you expect here. For instance when I run your code I get the following XML back:

<?xml version="1.0" encoding="utf-8"?>
<data>
   <status>error</status>
   <errorCode>00001</errorCode>
   <errorMessage>Request quota has been exceeded.  Please contact support.</errorMessage><errorMethod></errorMethod>
   <errorModule>api</errorModule>
</data>

You need to check the status element first.

if(xDoc.Root.Element("status").ToString().ToLower() == "success") 
    //run your code
heads5150
  • 7,263
  • 3
  • 26
  • 34
0

No one else can debug your code properly, because you are calling a url which we cannot determine (you have the unknown variables latitude and longitude.

I've debugged the line xDoc.Root.Element("forecast").Element("pop").Value, where xDoc is created from the long xml text you gave, and there's no exception. So I can suggest the following.

  1. Make sure n.Result is what you think it is. Log it somewhere to make sure it's not a blank string, or a json string, or anything else unexpected.

  2. Try to find out exactly where the null reference exception is occurring. To do this, unchain all those method calls, and have one method call per line:

    var root = xDoc.Root;
    var forecast = root.Element("forecast");
    var pop = forecast.Element("pop");
    var popValue = pop.Value;
    tempnow.Text = popValue;
    

Then at least you'll know exactly where you exception is coming from.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
polytopia
  • 419
  • 4
  • 11
  • It seems to be coming from .Element("pop");. I suspect Latitude/Longitude could be somehow be related to this problem, for example if it is being executed after the DownloadStringAsync part, and therefore my web service could be giving pop a null value. – Kevin Jan 30 '14 at 03:44