0
public partial class Form1 : Form
{
   int y = 0;
   string url = @"http://www.google.co.il";
   string urls = @"http://www.bing.com/images/search?q=cat&go=&form=QB&qs=n";

   public Form1()
   {
       InitializeComponent();
       //webCrawler(urls, 3);
       List<string> a = webCrawler(urls, 1);
       //GetAllImages();
   }

   private int factorial(int n)
   {
      if (n == 0) return 1;
      else y = n * factorial(n - 1);
      listBox1.Items.Add(y);
      return y;
   }

   private List<string> getLinks(HtmlAgilityPack.HtmlDocument document)
   {
       List<string> mainLinks = new List<string>();

       if (document.DocumentNode.SelectNodes("//a[@href]") == null)
       { }

       foreach (HtmlNode link in document.DocumentNode.SelectNodes("//a[@href]"))
       {
           var href = link.Attributes["href"].Value;
           mainLinks.Add(href);
       }

       return mainLinks;
   }

   private List<string> webCrawler(string url, int levels)
   {
      HtmlAgilityPack.HtmlDocument doc;
      HtmlWeb hw = new HtmlWeb(); 

      List<string> webSites;// = new List<string>();
      List<string> csFiles = new List<string>();

      csFiles.Add("temp string to know that something is happening in level = " + levels.ToString());
      csFiles.Add("current site name in this level is : "+url);
      /* later should be replaced with real cs files .. cs files links..*/

      doc = hw.Load(url);
      webSites = getLinks(doc);

      if (levels == 0)
      {
         return csFiles;
      }
      else
      {
         int actual_sites = 0;

         for (int i = 0; i < webSites.Count() && i< 100000; i++) // limiting ourseleves for 20 sites for each level for now..
         //or it will take forever.
         {
             string t = webSites[i];
             /*
                    if (!webSites.Contains(t))
                    {
                        webCrawler(t, levels - 1);
                    }
             */

             if ( (t.StartsWith("http://")==true) || (t.StartsWith("https://")==true) ) // replace this with future FilterJunkLinks function
             {
                actual_sites++;
                csFiles.AddRange(webCrawler(t, levels - 1));
                richTextBox1.Text += t + Environment.NewLine;
             }
          }

          // report to a message box only at high levels..
          if (levels==1)
             MessageBox.Show(actual_sites.ToString());

          return csFiles;
       }                
    }

在将几个站点发送到该getLinks函数后引发异常。

例外是在getLinks函数就行了:

foreach (HtmlNode link in document.DocumentNode.SelectNodes("//a[@href]"))

你调用的对象是空的

我尝试在那里使用 IF 来检查它是否为空,然后我做return mainLinks;了一个列表。

但如果我这样做,我不会从网站上获得所有链接。

现在我在构造函数中使用 url,如果我使用 url ( www.google.co.il),几秒钟后我会得到同样的异常。

我不知道为什么会抛出这个异常。这个例外有什么原因吗?

System.NullReferenceException 未处理
Message=Object 引用未设置为对象的实例。
Source=GatherLinks
StackTrace:
位于 D:\C-Sharp\GatherLinks\GatherLinks\GatherLinks\Form1.cs 中的 GatherLinks.Form1.getLinks(HtmlDocument 文档):D
中 GatherLinks.Form1.webCrawler(String url, Int32 级别) 的第 55 行:\C-Sharp\GatherLinks\GatherLinks\GatherLinks\Form1.cs:第 76 行
GatherLinks.Form1.webCrawler(String url, Int32 levels) in D:\C-Sharp\GatherLinks\GatherLinks\GatherLinks\Form1.cs:line 104
在 D:\C-Sharp\GatherLinks\GatherLinks\GatherLinks\Form1.cs
中的 GatherLinks.Form1..ctor():D:\C-Sharp\GatherLinks\GatherLinks\ 中 GatherLinks.Program.Main() 的第 29 行GatherLinks\Program.cs:第 18 行
在 System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
在 System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
在 System.Threading。 ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()

4

1 回答 1

4

问题似乎是你正在测试 null 但然后什么也没做 - 这里

            if (document.DocumentNode.SelectNodes("//a[@href]") == null)
            {
            }

我怀疑您想处理 null 情况,但还没有编写代码来执行此操作。你可能想要这样的东西:

    private List<string> getLinks(HtmlAgilityPack.HtmlDocument document)
        {
           List<string> mainLinks = new List<string>();
           if (document.DocumentNode.SelectNodes("//a[@href]") != null)
            {

                foreach (HtmlNode link in document.DocumentNode.SelectNodes("//a[@href]"))
                {
                    var href = link.Attributes["href"].Value;
                    mainLinks.Add(href);
                }
            }
            return mainLinks;
        }

你可能想整理一下更像:

   private List<string> getLinks(HtmlAgilityPack.HtmlDocument document)
        {
           List<string> mainLinks = new List<string>();
           var linkNodes = document.DocumentNode.SelectNodes("//a[@href]");
           if (linkNodes != null)
            {
                foreach (HtmlNode link in linkNodes)
                {
                    var href = link.Attributes["href"].Value;
                    mainLinks.Add(href);
                }
            }
            return mainLinks;
        }
于 2012-05-16T11:14:33.940 回答