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を使用して、そのnullかどうかを確認しようとしました。次にreturn mainLinks;、リストです。
しかし、私がそれをしている場合、私はウェブサイトからすべてのリンクを取得していません。
url(www.google.co.il)を使用している場合、コンストラクターでurlを使用しています。数秒後に同じ例外が発生します。
この例外がスローされる理由がわかりません。この例外の理由はありますか?
System.NullReferenceExceptionが未処理でした
Message=オブジェクト参照がオブジェクトのインスタンスに設定されていません。
Source = GatherLinks
StackTrace: D:\ CのGatherLinks.Form1.getLinks(HtmlDocument document)-D のGatherLinks.Form1.webCrawler(String url、Int32レベル)
のSharp \ GatherLinks \ GatherLinks \ GatherLinks \ Form1.cs:line 55
:\ C-Sharp \ GatherLinks \ GatherLinks \ GatherLinks \ Form1.cs:line 76
at GatherLinks.Form1.webCrawler(String url、Int32 level)in D:\ C-Sharp \ GatherLinks \ GatherLinks \ GatherLinks \ Form1.cs:line 104
D:\ C-Sharp \ GatherLinks \ GatherLinks \ GatherLinks \ Form1.cs:line 29
のGatherLinks.Form1..ctor()D:\ C-Sharp \ GatherLinks \ GatherLinks \のGatherLinks.Program.Main() GatherLinks \ Program.cs:line 18
System.AppDomain._nExecuteAssembly(Assembly assembly、String [] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile、Evidence assemblySecurity、String [] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
atSystem.Threading。 System.Threading.ThreadHelper.ThreadStart()
のSystem.Threading.ExecutionContext.Run(ExecutionContextexecutionContext、ContextCallbackコールバック、オブジェクト状態)のThreadHelper.ThreadStart_Context(オブジェクト状態
)