C# 用richtextbox控件 运行时“未将对象引用设置到对象的实例” 本帖最后由 King_returns 于 2009-12-25 15:03:45 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 看下你的代码当向richtextbox 中显示字符时 ?? 代码如下:我开打一个文件 ,能在richtextbox中显示,但是会出现 这个提示“未将对象引用设置到对象的实例”private void openToolStripMenuItem_Click(object sender, EventArgs e) { OpenFileDialog file = new OpenFileDialog(); richTextBox1.Text = " "; file.FileName = ""; file.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);//为了获取特定的系统文件夹,可以使用System.Environment类的静态方法GetFolderPath()。 file.Filter = "Text Document(*.txt)|*.txt|Html Document (*.html)|*.html|All Files|*.*"; file.FilterIndex = 2; file.ValidateNames = true; //文件有效性验证ValidateNames,验证用户输入是否是一个有效的Windows文件名 file.CheckFileExists = true; //验证路径有效性 file.CheckPathExists = true; //验证文件有效性 try { if (file.ShowDialog() == DialogResult.OK) { StreamReader str = new StreamReader(file.FileName, System.Text.Encoding.Default); string strWebText = str.ReadToEnd(); // this.textBox3.Text = strWebText; // this.Text = file.FileName; beginParseHtml(strWebText); viewParseHtml(strWebText); } } catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); } } public void beginParseHtml(string html) { parserHtml bph = new parserHtml(); BitTokenContext bitTokenContext = bph.Parse(html); BitTokenList ptokenList = bitTokenContext.TokenList; while ((ptokenList = ptokenList.Next) != null) { if (ptokenList.Token.IsEnd) { richTextBox1.Text = richTextBox1.Text + "/" + ptokenList.Token.Type.ToString() + "\r\n"; } else { richTextBox1.Text = richTextBox1.Text + ptokenList.Token.Type.ToString() + "\r\n"; } if (ptokenList.Token.Type == HTMLElement.HTML_TEXT)// 显示TEXT { richTextBox1.Text = richTextBox1.Text + " " + ptokenList.Token.Data + "\r\n"; } BitTokenAttrList tokenAttrList = ptokenList.Token.AttrList; while ((tokenAttrList = tokenAttrList.Next) != null) { // richTextBox1.Text = richTextBox1.Text + " " + tokenAttrList.Attr.Type + "=" + tokenAttrList.Attr.Value + "\r\n"; }//while ((tokenAttrList = tokenAttrList.Next) != null) }//while ((tokenList = tokenList.Next) != null) }//beginParseHtml "未将对象引用设置到对象的实例" 这个错误一般是因为你的值是NULL引起的 我在网上查了 说是什么值 是NULL ,我不知道 那里 错了,知道的 麻烦帮我 ,看下哪里有问题? 可以运行,打开文件也可以将文件中的数据处理后显示到richtextbox中,就是会提示:未将对象引用设置到对象的实例。逐语句 运行也没有错误。。 我这样改的,还是不能if(richTextBox1!=null){ while ((ptokenList = ptokenList.Next) != null) { ........... }} 没有NEW 吗?按说richtextbox直接拖上去的啊!不可能出现这个现象啊!你把那个richTextBox1 = new richTextBox();删了吗? 支持3楼的说法,判断一下你要加的值是否为空不是判断richtextbox为空 正则表达式 [200分]无线网卡上网问题. 如何在控件中使用C#中分配的内存? c#电梯实现 xml转成dataset之后,能用ado.net中ExecuteNonQuery 和sql语句访问、修改DataSet.Tables[0]中的记录吗? 请教达人:想使用sybase的数据库,是不是非得用odbc连接?又应该怎么去写呢? 如何将某窗口设置为另一窗口的子窗口? c# 获取 ListView20WndClass 里的数据问题 ■■■有没有右键菜单也像VS.NET风格的???■■■ 一个奇怪的参数问题 数组 Form中如何添加一个ActiveX控件?
当向richtextbox 中显示字符时 ??
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
OpenFileDialog file = new OpenFileDialog();
richTextBox1.Text = " ";
file.FileName = ""; file.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);//为了获取特定的系统文件夹,可以使用System.Environment类的静态方法GetFolderPath()。 file.Filter = "Text Document(*.txt)|*.txt|Html Document (*.html)|*.html|All Files|*.*";
file.FilterIndex = 2;
file.ValidateNames = true; //文件有效性验证ValidateNames,验证用户输入是否是一个有效的Windows文件名 file.CheckFileExists = true; //验证路径有效性 file.CheckPathExists = true; //验证文件有效性
try
{
if (file.ShowDialog() == DialogResult.OK)
{
StreamReader str = new StreamReader(file.FileName, System.Text.Encoding.Default); string strWebText = str.ReadToEnd();
// this.textBox3.Text = strWebText;
// this.Text = file.FileName; beginParseHtml(strWebText);
viewParseHtml(strWebText);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
public void beginParseHtml(string html)
{
parserHtml bph = new parserHtml(); BitTokenContext bitTokenContext = bph.Parse(html);
BitTokenList ptokenList = bitTokenContext.TokenList;
while ((ptokenList = ptokenList.Next) != null)
{
if (ptokenList.Token.IsEnd)
{
richTextBox1.Text = richTextBox1.Text + "/" + ptokenList.Token.Type.ToString() + "\r\n";
}
else
{
richTextBox1.Text = richTextBox1.Text + ptokenList.Token.Type.ToString() + "\r\n"; }
if (ptokenList.Token.Type == HTMLElement.HTML_TEXT)// 显示TEXT
{
richTextBox1.Text = richTextBox1.Text + " " + ptokenList.Token.Data + "\r\n"; }
BitTokenAttrList tokenAttrList = ptokenList.Token.AttrList; while ((tokenAttrList = tokenAttrList.Next) != null)
{
//
richTextBox1.Text = richTextBox1.Text + " " + tokenAttrList.Attr.Type + "=" + tokenAttrList.Attr.Value + "\r\n"; }//while ((tokenAttrList = tokenAttrList.Next) != null)
}//while ((tokenList = tokenList.Next) != null)
}//beginParseHtml
我在网上查了 说是什么值 是NULL ,我不知道 那里 错了,知道的 麻烦帮我 ,看下哪里有问题?
{
while ((ptokenList = ptokenList.Next) != null)
{
...........
}}