总是出现上面的问题。显示为:未处理的“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)
{
}
中断在 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)
{
}
解决方案 »
- 关于.Net网页开发中ajax方法
- 在做一个关于电子秤数据分析的程序,遇到数据分析问题,寻求帮助!
- 谁能给我解释一下?
- scintillanet 控件说明文档谁有 或者哪位高人用过这个控件给个例子学习一下
- 如何实现datagridview行的颜色
- 如何给一个自定义控件 加上 datasource 功能,要怎么实现的
- duwamish的monitor用法,请各位指点。。
- 无边框窗体的拖动问题,如何做成象win form 一样的效果?UP 有分!
- 谁有Installshield8.02 的安装密码??就是开始运行时候的那个
- 我有个很奇怪的问题
- 接口问题
- 应该把哪些代码写在构造函数里,哪些代码写在OnLoad()里呢?
{
this.rtfText = new System.Windows.Forms.RichTextBox();
}
catch (NullReferenceException ex)
{
}如果捕捉到了才能说明问题在这里。一般来说,初始化应该不会错误。
return;
else
oldFont = this.rtfText.SelectionFont;
我用了这语判断还是不行。
进行单步调试,到this.rtfText = new System.Windows.Forms.RichTextBox();这句就中断啦,进行不下去了。实在是不知道怎么回事啊!
问题显示为:未处理的“System.NullReferenceException”类型的异常出现在 system.windows.forms.dll 中。 其他信息: 未将对象引用设置到对象的实例。
1、ViewState 对象为Null。
2、DateSet 空。
3、sql语句或Datebase的原因导致DataReader空。
4、声明字符串变量时未赋空值就应用变量。
5、未用new初始化对象。
6、Session对象为空
估计你的属于 5 ,你在仔细检查下代码,单步跟踪下,看看程序的执行顺序可能与你所设想的不一样!!!!
这个厉害
//////////////////////////////////////////////////////
未将对象引用设置到对象的实例异常的原因
1、ViewState 对象为Null。
2、DateSet 空。
3、sql语句或Datebase的原因导致DataReader空。
4、声明字符串变量时未赋空值就应用变量。
5、未用new初始化对象。
6、Session对象为空
估计你的属于 5 ,你在仔细检查下代码,单步跟踪下,看看程序的执行顺序可能与你所设想的不一样!!!!