错误“对象引用未设置为对象的实例” [英] Error “Object reference not set to an instance of an object”

查看:93
本文介绍了错误“对象引用未设置为对象的实例”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码可以与其中一个网站一起使用,但是有些网站会回复这样的错误信息,我不知道如何编辑(星星错误)



This code can work with one of the web, but with some sites it back error messages like this, I do not know how to edit (Error in stars)

var document = webBrowser1.Document;
            var documentAsIHtmlDocument3 = (mshtml.IHTMLDocument3)document.DomDocument;

            var htmlString = documentAsIHtmlDocument3.documentElement.innerHTML;

            HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
            doc.LoadHtml(htmlString);

            //Sử dụng node để lấy tin
           HtmlNodeCollection texts = doc.DocumentNode.SelectNodes("//div[@id='divTop']/p");
            string kq = "";
            // cho vòng lặp để lấy kết quả
            **foreach** (var item in texts)
            {
                kq += item.InnerText + Environment.NewLine;
            }

            richTextBox1.Text = kq;





代码PHP:





code PHP:

<div id="divTop">
    <div id="text-conent">
         style="width: 500px; 
                float: right;" >
    </div>
    <div id="grid">
         style="margin-removed 505px; 
                height: 700px;" >
    </div>
  </div>

推荐答案

问题是你的DocumentNode.SelectNodes行返回null,你试图枚举它。您的代码假定SelectNodes调用返回的正是您所期望的但却没有,并且您得到了您所看到的错误。



这真的是最简单的跟踪错误。您所要做的就是在调试器停止时查看变量的值并向您显示异常。查看失败的行中涉及的变量并向后跟随您的代码。
The problem is your DocumentNode.SelectNodes line returned null and you're trying to enumerate it. Your code assumes that the SelectNodes call returned exactly what you were expecting but it didn't and you get the error that you're seeing.

This is really the easiest error to track down. All you have to do is look at the values of the variables when the debugger stops and shows you the exception. Look at the variables that are involved in the line that failed and follow your code backwards.


您的string =kq值不会传递到变量中通过断点检查您的连接..
your string="kq" value does not pass into the variables check your connection through breakpoints..


但我不明白,当我使用后面的simple_html_dom获取div标签中的信息时,所以现在使用DocumentNode.SelectNodes返回null?



but I do not understand, when I use the back simple_html_dom obtain information in div tags, so now use DocumentNode.SelectNodes returns null ?

include 'simple_html_dom.php';
if(isset(


这篇关于错误“对象引用未设置为对象的实例”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆