小菜同学 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 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。