小菜同学 2014-05-12 14:31 采纳率: 0%
浏览 4734

用户代码未处理 System.ArgumentNullException

用户代码未处理 System.ArgumentNullException
HResult=-2147467261
Message=值不能为 null。
参数名: String Source=mscorlib
ParamName=String
StackTrace:
在 System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
在 System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)
在 NetOpinion.Form2.webBrowser1_DocumentCompleted(Object sender, WebBrowserDocumentCompletedEventArgs e) 位置 d:\ProgrammingCode\NetOpinion1.2\NetOpinion\Form2.cs:行号 92
InnerException:

代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.Collections.Specialized;

namespace NetOpinion
{
public partial class Form2 : Form
{
int ipage,pagenum;
string text;
NameValueCollection nvc; //解析Url用

    VR vr = new VR();
    DataItem di = new DataItem();
    SqlServer db = new SqlServer();

    int start;
    int final;
    string httpbase = "http://www.tianya.cn/new/publicforum/articleslist.asp?pageno=1&stritem=";

    public Form2()
    {
        ipage = 1;
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        if (toolStripComboBox1.Text == "娱乐—娱乐八卦")
        {
            start = 9;
            final = 108;
            toolStripTextBox2.Text = httpbase + "funinfo&part=1";
        }
        else if (toolStripComboBox1.Text == "社会—天涯杂谈")
        {
            start = 9;
            final = 108;
            toolStripTextBox2.Text = httpbase + "free&part=1";
        }
        else if (toolStripComboBox1.Text == "社会—国际观察")
        {
            start = 9;
            final = 53;
            toolStripTextBox2.Text = httpbase + "worldlook&part=1";
        }
        else if (toolStripComboBox1.Text == "情感—情感天地")
        {
            start = 9;
            final = 53;
            toolStripTextBox2.Text = httpbase + "feeling&part=1";
        }
        else
        {
            MessageBox.Show("请选择论坛版面");
            return;
        }

        if (toolStripTextBox1.Text == "")
        {
            MessageBox.Show("请输入抓取页数");
            return;
        }

        if (toolStripComboBox2.Text == "选择时刻")
        {
            MessageBox.Show("请选择时刻");
            return;
        }
        text = toolStripComboBox2.Text;
        pagenum = int.Parse(toolStripTextBox1.Text);

        webBrowser1.Navigate(toolStripTextBox2.Text, "_self");
    }

    private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
        if (webBrowser1.Document.Url != e.Url || e.Url.AbsoluteUri == "about:blank")
        {
            return;
        }
        ParseUrl(webBrowser1.Url.AbsoluteUri, out nvc);


        if (nvc.Count != 0 && int.Parse(nvc.Get("pageNO")) > pagenum)
        {
            toolStripStatusLabel2.Text = "恭喜你!页面分析完毕,你可以查看结果了";
            return;
        }

        toolStripStatusLabel2.Text = "当前正在抓取处理的页" + ipage.ToString() + "...";

        ParsePage(webBrowser1.Document,start,final);
    }

    public void ParseUrl(string url, out NameValueCollection nvc)  //解析网址,正则匹配
    {
        nvc = new NameValueCollection();

        if (url == "")
        {
            return;
        }
        int questionMarkIndex = url.IndexOf('?');
        if (questionMarkIndex == -1 || questionMarkIndex == url.Length - 1)
        {

            return;
        }
        string ps = url.Substring(questionMarkIndex + 1);

        // 开始分析参数对
        Regex re = new Regex(@"(^|&)?(\w+)=([^&]+)(&|$)?", RegexOptions.Compiled);
        MatchCollection mc = re.Matches(ps);

        foreach (Match m in mc)
        {
            nvc.Add(m.Result("$2"), m.Result("$3"));
        }
    }

    public void ParsePage(HtmlDocument documentShare,int start,int final)    //解析网页
    {
        int table = 1;
        foreach (HtmlElement htmelt in documentShare.GetElementsByTagName("table"))
        {
            if ( table>=start && table<=final )
            {
                HtmlElement htmtr = htmelt.GetElementsByTagName("tr")[0];
                GetTable(htmelt);
                UpdateDataBase();
            }
            table++;
        }       
        foreach (HtmlElement htmelt in documentShare.GetElementsByTagName("a"))
        {
            if (htmelt.InnerText == "下一页")
            {
                htmelt.InvokeMember("click");
                ipage++;
            }
        }
    }
}

}

  • 写回答

2条回答

  • feiyun0112 2014-05-13 01:08
    关注

    调试看看
    nvc.Get("pageNO")的值不对

    评论

报告相同问题?

悬赏问题

  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘