Richtextbox对象引用未设置为对象的实例

时间:2013-06-18 03:33:58

标签: c# object reference richtextbox instance

为什么我收到此错误?我的程序使用的是.NET4 Framework。我在下面得到了这个错误:对象引用没有设置为对象的实例。

   if (this.webBrowser1.Url.AbsoluteUri.Contains("/fire/") && this.richTextBox1.Text.Contains("-"))
            this.richTextBox1.Text = this.richTextBox1.Text.Substring(0, this.richTextBox1.Text.IndexOf('-'));

因此我对其进行了修改并将其更改为以下内容,但仍然无效:

               if (webBrowser1.Url.AbsoluteUri.Contains("/fire/") && richTextBox1.Text.Contains("-"))  {richTextBox1.Text = richTextBox1.Text.Substring(0, richTextBox1.Text.IndexOf('-'));}

以下是该代码的真实主体:

   private void button64_Click(object sender, EventArgs e)
    {

        this.richTextBox2.Refresh();
        this.richTextBox1.Refresh();
        if (object.Equals((object)this.richTextBox1.Text, (object)this.richTextBox2.Text))
            this.label1.BackColor = Color.GreenYellow;
        if (!object.Equals((object)this.richTextBox1.Text, (object)this.richTextBox2.Text))
            this.label1.BackColor = Color.Orchid;

        if (this.webBrowser1.Url.AbsoluteUri.Contains("/fire/") && this.richTextBox1.Text.Contains("-"))
            this.richTextBox1.Text = this.richTextBox1.Text.Substring(0, this.richTextBox1.Text.IndexOf('-'));
        if (this.webBrowser1.Url.AbsoluteUri.Contains("hell.com/webapp/wcs/stores/servlet/OrderSummaryDisplay?") && this.richTextBox1.Text.Contains(","))
            this.richTextBox1.Text = this.richTextBox1.Text.Substring(0, this.richTextBox1.Text.IndexOf(','));
        if (this.webBrowser1.Url.AbsoluteUri.Contains("hell.com/webapp/wcs/stores/servlet/OrderSummaryDisplay?") && this.richTextBox2.Text.Contains(","))
            this.richTextBox2.Text = this.richTextBox2.Text.Substring(0, this.richTextBox2.Text.IndexOf(','));
        if (this.richTextBox1.Text.Contains("-") && !this.richTextBox2.Text.Contains("-"))
            this.richTextBox2.Text = this.richTextBox2.Text.Substring(0, this.richTextBox2.Text.IndexOf('-'));
        if (!this.richTextBox2.Text.Contains("-") || this.richTextBox1.Text.Contains("-"))
            return;
        this.richTextBox2.Text = this.richTextBox2.Text.Substring(0, this.richTextBox2.Text.IndexOf('-'));

    }

1 个答案:

答案 0 :(得分:0)

您的问题中没有足够的细节可以肯定,但是当您尝试访问对象的属性为空时,您会收到该错误。

在您指定的代码行上放置断点并检查每个对象以查看它是否为空,或者更改代码以检查空值:

if (webBrowser1.Url != null && webBrowser1.Url.AbsoluteUri.Contains("/fire/") && .... )