总是出现上面的问题。显示为:未处理的“System.NullReferenceException”类型的异常出现在 system.windows.forms.dll 中。其他信息: 未将对象引用设置到对象的实例。
 
中断在 this.rtfText = new System.Windows.Forms.RichTextBox();这句上下面是代码(一部分)
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace RichTextBoxTest
{ public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button btnBold;
private System.Windows.Forms.Button btnItalic;
private System.Windows.Forms.Button btnUnderline;
private System.Windows.Forms.Button btnCenter;
private System.Windows.Forms.Label lblSize;
private System.Windows.Forms.TextBox txtSize;
private System.Windows.Forms.RichTextBox rtfText;
private System.Windows.Forms.Button btnLoad;
private System.Windows.Forms.Button btnSave;

private System.ComponentModel.Container components = null; public Form1()
{
InitializeComponent();

this.txtSize.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.txtSize_KeyPress);
this.txtSize.Validating += new System.ComponentModel.CancelEventHandler(this.txtSize_Validating);
this.rtfText.LinkClicked += new System.Windows.Forms.LinkClickedEventHandler(this.rtfText_LinkedClick);
}
private void txtSize_KeyPress(object sender,System.Windows.Forms.KeyPressEventArgs e)
{
if((e.KeyChar<48 || e.KeyChar>57) && e.KeyChar != 8 && e.KeyChar != 13)
{
e.Handled = true;
}
else if(e.KeyChar == 13)
{
TextBox txt = (TextBox)sender;
if(txt.Text.Length > 0)
ApplyTextSize(txt.Text);
e.Handled = true;
this.rtfText.Focus();
}

}
private void txtSize_Validating(object sender,System.ComponentModel.CancelEventArgs e)
{
TextBox txt = (TextBox)sender;
ApplyTextSize(txt.Text);
this.rtfText.Focus();
}
private void ApplyTextSize(string textSize)
{
float newSize = Convert.ToSingle(textSize);
FontFamily currentFontFamily;
Font newFont;

currentFontFamily = this.rtfText.SelectionFont.FontFamily;
newFont = new Font(currentFontFamily,newSize);

this.rtfText.SelectionFont = newFont;
}
private void rtfText_LinkedClick(object sender,System.Windows.Forms.LinkClickedEventArgs e)
{
System.Diagnostics.Process.Start(e.LinkText);
}

protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null) 
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows 窗体设计器生成的代码
private void InitializeComponent()
{
this.btnBold = new System.Windows.Forms.Button();
this.btnItalic = new System.Windows.Forms.Button();
this.btnUnderline = new System.Windows.Forms.Button();
this.btnCenter = new System.Windows.Forms.Button();
this.lblSize = new System.Windows.Forms.Label();
this.txtSize = new System.Windows.Forms.TextBox();
this.rtfText = new System.Windows.Forms.RichTextBox();
this.btnLoad = new System.Windows.Forms.Button();
this.btnSave = new System.Windows.Forms.Button();
this.SuspendLayout();
//
                           //btnBold
                           //
this.btnBold.Anchor = System.Windows.Forms.AnchorStyles.Top;
this.btnBold.Location = new System.Drawing.Point(8, 56);
this.btnBold.Name = "btnBold";
this.btnBold.TabIndex = 0;
this.btnBold.Text = "Bold";
this.btnBold.Click += new System.EventHandler(this.btnBold_Click);
// 
// btnItalic
// 
this.btnItalic.Anchor = System.Windows.Forms.AnchorStyles.Top;
this.btnItalic.Location = new System.Drawing.Point(104, 56);
this.btnItalic.Name = "btnItalic";
this.btnItalic.TabIndex = 1;
this.btnItalic.Text = "Italic";
this.btnItalic.Click += new System.EventHandler(this.btnItalic_Click);
// 
// btnUnderline
// 
this.btnUnderline.Anchor = System.Windows.Forms.AnchorStyles.Top;
this.btnUnderline.Location = new System.Drawing.Point(200, 56);
this.btnUnderline.Name = "btnUnderline";
this.btnUnderline.TabIndex = 2;
this.btnUnderline.Text = "Underline";
this.btnUnderline.Click += new System.EventHandler(this.btnUnderline_Click);
// 
// btnCenter
// 
this.btnCenter.Anchor = System.Windows.Forms.AnchorStyles.Top;
this.btnCenter.Location = new System.Drawing.Point(296, 56);
this.btnCenter.Name = "btnCenter";
this.btnCenter.TabIndex = 3;
this.btnCenter.Text = "Center";
this.btnCenter.Click += new System.EventHandler(this.btnCenter_Click);
// 
// lblSize
// 
this.lblSize.Anchor = System.Windows.Forms.AnchorStyles.Top;
this.lblSize.Location = new System.Drawing.Point(120, 104);
this.lblSize.Name = "lblSize";
this.lblSize.Size = new System.Drawing.Size(64, 23);
this.lblSize.TabIndex = 4;
this.lblSize.Text = "Size";
this.lblSize.Click += new System.EventHandler(this.label1_Click);
// 
// txtSize
// 
this.txtSize.Anchor = System.Windows.Forms.AnchorStyles.Top;
this.txtSize.Location = new System.Drawing.Point(200, 104);
this.txtSize.Name = "txtSize";
this.txtSize.TabIndex = 5;
this.txtSize.Text = "10";
// 
// rtfText
// 
this.rtfText.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
| System.Windows.Forms.AnchorStyles.Left) 
| System.Windows.Forms.AnchorStyles.Right)));
this.rtfText.Location = new System.Drawing.Point(24, 144);
this.rtfText.Name = "rtfText";
this.rtfText.Size = new System.Drawing.Size(336, 160);
this.rtfText.TabIndex = 6;
this.rtfText.Text = "";
// 
// btnLoad
// 
this.btnLoad.Anchor = System.Windows.Forms.AnchorStyles.Bottom;
this.btnLoad.Location = new System.Drawing.Point(64, 320);
this.btnLoad.Name = "btnLoad";
this.btnLoad.TabIndex = 7;
this.btnLoad.Text = "Load";
this.btnLoad.Click += new System.EventHandler(this.btnLoad_Click);
// 
// btnSave
// 
this.btnSave.Anchor = System.Windows.Forms.AnchorStyles.Bottom;
this.btnSave.Location = new System.Drawing.Point(192, 320);
this.btnSave.Name = "btnSave";
this.btnSave.TabIndex = 8;
this.btnSave.Text = "Save";
this.btnSave.Click += new System.EventHandler(this.btnSave_Click);
// 
// Form1
// 
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(376, 358);
this.Controls.Add(this.btnSave);
this.Controls.Add(this.btnLoad);
this.Controls.Add(this.rtfText);
this.Controls.Add(this.txtSize);
this.Controls.Add(this.lblSize);
this.Controls.Add(this.btnCenter);
this.Controls.Add(this.btnUnderline);
this.Controls.Add(this.btnItalic);
this.Controls.Add(this.btnBold);
this.MinimumSize = new System.Drawing.Size(384, 392);
this.Name = "Form1";
this.Text = "RichTextBoxTest";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false); }
#endregion
                  [STAThread]
static void Main() 
{
Application.Run(new Form1());
} private void label1_Click(object sender, System.EventArgs e)
{

} private void btnBold_Click(object sender, System.EventArgs e)
{
Font oldFont;
Font newFont;
oldFont = this.rtfText.SelectionFont;
if(oldFont.Bold)
newFont = new Font(oldFont,oldFont.Style & ~FontStyle.Bold);
else
newFont = new Font(oldFont,oldFont.Style | FontStyle.Bold);

this.rtfText.SelectionFont = newFont;
this.rtfText.Focus();
} private void Form1_Load(object sender, System.EventArgs e)
{

}

解决方案 »

  1.   

    System.NullReferenceException很明显,你在某个地方没有赋值就直接用了变量。Debug吧,会告诉你具体位置的。代码太长了。
      

  2.   

    在IDE中把rtfText删除掉 重新拖一个进来
      

  3.   

    你用try
    {
    this.rtfText = new System.Windows.Forms.RichTextBox();
    }
    catch (NullReferenceException ex)
    {
    }如果捕捉到了才能说明问题在这里。一般来说,初始化应该不会错误。
      

  4.   

    学会自己debug 调试吧 ,这么长的代码,看下来的人也没有几个
      

  5.   

    oldFont = this.rtfText.SelectionFont;确定this.rtfText.SelectionFont; 不是null?  可能问题就出在此处判断一下 为null的话,reutrn
      

  6.   

    if(this.rtfText.SelectionFont == null)
        return;
    else
        oldFont = this.rtfText.SelectionFont;
    我用了这语判断还是不行。
    进行单步调试,到this.rtfText = new System.Windows.Forms.RichTextBox();这句就中断啦,进行不下去了。实在是不知道怎么回事啊!
      

  7.   

    使用RichTextBox这个控件的时候,在编译时其初始化那句this.rtfText = new System.Windows.Forms.RichTextBox();总是通不过
    问题显示为:未处理的“System.NullReferenceException”类型的异常出现在 system.windows.forms.dll 中。 其他信息: 未将对象引用设置到对象的实例。 
      

  8.   

    未将对象引用设置到对象的实例异常的原因
    1、ViewState 对象为Null。 
    2、DateSet 空。 
    3、sql语句或Datebase的原因导致DataReader空。 
    4、声明字符串变量时未赋空值就应用变量。 
    5、未用new初始化对象。 
    6、Session对象为空
    估计你的属于  5  ,你在仔细检查下代码,单步跟踪下,看看程序的执行顺序可能与你所设想的不一样!!!!
      

  9.   


    这个厉害
    //////////////////////////////////////////////////////
    未将对象引用设置到对象的实例异常的原因
    1、ViewState 对象为Null。
    2、DateSet 空。
    3、sql语句或Datebase的原因导致DataReader空。
    4、声明字符串变量时未赋空值就应用变量。
    5、未用new初始化对象。
    6、Session对象为空
    估计你的属于 5 ,你在仔细检查下代码,单步跟踪下,看看程序的执行顺序可能与你所设想的不一样!!!!