开发者

How can this error be possible in this code - error : Object reference not set to an instance of an object

开发者 https://www.devze.com 2023-04-13 03:28 出处:网络
This is how i call that error given function var开发者_JAVA技巧 CrawlPage = Task.Factory.StartNew(() =>

This is how i call that error given function

    var开发者_JAVA技巧 CrawlPage = Task.Factory.StartNew(() =>
{
    return crawlPage(srNewCrawledUrl);
});

var GetLinks = CrawlPage.ContinueWith(resultTask =>
{
    if (CrawlPage.Result == null)
    {
        return null;
    }
    else
    {
        return ReturnLinks(CrawlPage.Result, srNewCrawledUrl, srNewCrawledPageId);
    }

});

This is the error i really don't understand how is that possible. I am using local assigned variables so variables should be thread safe for all threads. Am i incorrect ?

this is the error image :

How can this error be possible in this code - error : Object reference not set to an instance of an object


you better validate InnerHtml is null or not before calling

var GetLinks = CrawlPage.ContinueWith(resultTask =>
{
    if (CrawlPage.Result == null || CrawlPage.Result.DocumentNode == null ||  CrawlPage.Result.DocumentNode.InnerHtml == null)
    {
        return null;
    }
    else
    {
        return ReturnLinks(CrawlPage.Result, srNewCrawledUrl, srNewCrawledPageId);
    }

});

Or check this on ReturnLinks method


I am using local assigned variables so variables should be thread safe for all threads. Am i incorrect ?

The fact that you are making a reference local does not mean that the object this reference points to suddenly becomes local. Other thread (not shown in your question?) might still be mutating the HtmlDocument object at just the wrong moment (between hdDoc.DocumentNode != null and hdDoc.DocumentNode.InnerHtml != null).

0

精彩评论

暂无评论...
验证码 换一张
取 消