-1

I've written following method to extract needed html data from web page:

public async System.Threading.Tasks.Task<string> getdata()
{
    var localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
    HttpClient client = new HttpClient();
    HtmlDocument document = new HtmlDocument();
    byte[] htmlb = (await client.GetByteArrayAsync("http://<path-to-cgi>?passwd=" + (string)localSettings.Values["password"] + "&<param>=<hardcoded value>" + "&<param>=<hardcoded value>").ConfigureAwait(false));
    document.LoadHtml(Encoding.UTF8.GetString(htmlb,0,htmlb.Length));
    var inputs = document.DocumentNode.Descendants("div");
    HtmlNode elm = null;
    foreach (var input in inputs)
    {
        if (input.Attributes["id"].ToString() == "main")
        {
            elm = input;
        }
    }
    return (elm.InnerHtml);
}

And when I execute this method, I get exception that is mentioned above.

Joel Coehoorn
  • 362,140
  • 107
  • 528
  • 764
fedor2612
  • 331
  • 2
  • 6
  • You should really start with basic debugging on this, at least give us which line number, and preferably which variable is null, you will probably be able to figure out the solution on your own once you do this. – starlight54 Jul 23 '16 at 19:06
  • Which part of the exception message are you having difficulty with? Sounds like a fairly straight to the point bit of information. You are trying to access an object that doesn't exist. – IInspectable Jul 23 '16 at 19:08

1 Answers1

0

It looks like your if-condition might be returning false, and elm is not being assigned a value. In order to ensure a value is returned from your function, replace

return (elm.InnerHtml); 

with

if(elm != null){
return (elm.InnerHtml ?? "");
}
else{
return "";
}
Jace
  • 757
  • 4
  • 17