0

The program that I have created needs to read information from a website and then store it. I am getting the error:

System.ArgumentNullException: Value cannot be null.
Parameter name: source
at System.Linq.Enumerable.Select[TSource,TResult](IEnumerable1 source, Func2 selector)

However it does not always run in error. As in sometimes it works and sometimes it doesn't work. How can this be? Here is the code that is giving me the error line 4.

IEnumerable<string> webtemp = Enumerable.Empty<string>();
if (datastring.Contains("today_nowcard-temp"))
{
    webtemp = doc.DocumentNode.SelectNodes("//div[@class = 'today_nowcard-temp']/span").Select(d => d.InnerText.Trim());

    foreach (var this_header in webtemp)
    {
        string[] temporary = this_header.Trim().Replace("Â", "-").Replace(" ", "-").Split('-');
        int f = (Convert.ToInt32(temporary[0]));
        _actualData[0].temp = GetCelsius(f);
        //Console.WriteLine(_actualData[0].temp);
    }
}
rene
  • 41,474
  • 78
  • 114
  • 152
T.W.
  • 19
  • 1
  • 6
  • 3
    this `DocumentNode.SelectNodes("//div[@class = 'today_nowcard-temp']/span")` returns an null but without seeing what you have in `doc` it is impossible to say why that xpath doesn't return an result. – rene Aug 03 '17 at 16:17
  • It returns a result sometimes and from what I can tell the xpath doesn't change. – T.W. Aug 03 '17 at 16:18
  • We need to see the xml of your document is was @rene is getting at. – interesting-name-here Aug 03 '17 at 16:19
  • Looks like you're having a similar issues as this: https://stackoverflow.com/questions/38166949/unable-to-retrieve-temperature-value-from-html-using-beautifulsoup-module-python – rene Aug 03 '17 at 16:19
  • 1
    @T.W.: Chances are that even though your xpath is not changing that the document you are downloading is. – Chris Aug 03 '17 at 16:24
  • @xxbbcc: That question is for java. The C# equivalent is https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it but isn't directly applicable since that deals with NullReferenceExceptions and this is a ArgumentNullException. There are some useful comments in that questions's answers but its not an exact duplicate (IMO). – Chris Aug 03 '17 at 16:26
  • What is the type of `doc`? Is it from HtmlAgilityPack? – rene Aug 03 '17 at 16:30
  • @rene, I guess it is from HtmlAgilityPack. I have seen it there. – ManishKumar Aug 03 '17 at 16:32
  • Yeah its an HtmlAgilityPack. The document does change but that is why i had to check if it contained the class heading. so now the doc should be the same because that is where the doc was changing. – T.W. Aug 03 '17 at 16:38

1 Answers1

3

Reason behind this exception is the value that is returned by your SelectNodes method. Sometimes it returns null and then you try to perform Linq operation on null and it generate error. So you can perform a null check on this

var temp= doc.DocumentNode.SelectNodes("//div[@class = 'today_nowcard-temp']/span");

if(temp != null){
//TODO
}
ManishKumar
  • 1,476
  • 2
  • 14
  • 22