System.NullReferenceException 异常问题
public class meta:Page{
public meta()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public void metaadd(string mname, string mconten)
{
HtmlMeta mmeta = new HtmlMeta();
mmeta.Name = mname;
mmeta.Content = mconten;
this.Header.Controls.Add(mmeta);
}
}
cs:
meta newm = new meta();
newm.metaadd("title", titlen);
调试异常:
this.Header.Controls.Add(mmeta);出错
用户代码未处理 System.NullReferenceException
Message="未将对象引用设置到对象的实例。"
--------------------编程问答-------------------- 不是this.Header为空就是this.Header.Controls为空...至于哪个空为什么为空...自己去看其他部分的代码... --------------------编程问答-------------------- 没有其他代码.
aspx就是VS2005的ie6.0 的默认代码 --------------------编程问答-------------------- 不写在类里,放page_load是好的! --------------------编程问答-------------------- 没有其他代码了?
this.Header->Page.Header...你觉得Page.Header有可能有Controls吗? --------------------编程问答-------------------- 你有没有 runat=server 的 <header runat=‘server“> --------------------编程问答-------------------- 有的,写在page_load里,正常,封状成类,就有这个问题! --------------------编程问答-------------------- 没人回复了吗?? --------------------编程问答-------------------- web程序能单步调试吗?没有试过,如果可以单步调试的话,到了哪一步出错就可以找到是哪个地方造成空引用了 --------------------编程问答-------------------- 请参考:
#region PAGE HEAD
//Page meta information
public void BackHeadContent(HtmlControl htmlCtrl)
{
Content(htmlCtrl, "网站后台管理系统", "~/CssStyle/SiteStyles.css");
}
public void HeadContent(HtmlControl htmlCtrl, string strPageTitle)
{
Content(htmlCtrl, strPageTitle, "~/CssStyle/Styles.css");
}
private void Content(HtmlControl htmlCtrl, string strTitle,string cssFile)
{
//Title
HtmlTitle title = new HtmlTitle();
title.Text = strTitle;
htmlCtrl.Controls.Add(title);
//Link/CSSfile:
HtmlLink cssLink = new HtmlLink();
cssLink.Href = cssFile;
cssLink.Attributes.Add("rel", "stylesheet");
cssLink.Attributes.Add("type", "text/css");
htmlCtrl.Controls.Add(cssLink);
HtmlMeta
author = new HtmlMeta(), // author
copyright = new HtmlMeta(), // copyright
date = new HtmlMeta(), // date
keywords = new HtmlMeta(), // keywords
description = new HtmlMeta(), // description
robots = new HtmlMeta();// robots
author.Name = "Author";
author.Content = "Insus.NET";
copyright.Name = "Copyright";
copyright.Content = "Copyright 2008 Insus.NET";
date.Name = "date";
date.Content = DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToShortTimeString();
keywords.Name = "keywords";
keywords.Content = "Insus ";
description.Name = "description";
robots.Name = "robots";
robots.Content = "all";
string[] InsusWords = "VISUAL STUDIO 2003,VISUAL STUDIO 2005,Microsoft SQL Server 2005,ASP.NET,ASP.NET 2.0,VB.NET,C#,AJAX,LINQ"
.Replace("\\r", string.Empty)
.Replace("\\n", string.Empty)
.Replace(">br />", string.Empty)
.Replace(",", string.Empty)
.Replace("\\'", string.Empty)
.Split(' ');
foreach (string word in InsusWords)
keywords.Content += word + ",";
if (keywords.Content.ToString().Length > 1024)
{
keywords.Content = keywords.Content.Substring(0, keywords.Content.IndexOf(" ", 1024));
}
description.Content = "This web site use asp.net2.0 and C# and Ajax technology";
if (description.Content.ToString().Length > 1024)
{
description.Content = description.Content.Substring(0, description.Content.IndexOf(" ", 1024));
}
htmlCtrl.Controls.Add(author);
htmlCtrl.Controls.Add(copyright);
htmlCtrl.Controls.Add(date);
htmlCtrl.Controls.Add(keywords);
htmlCtrl.Controls.Add(description);
htmlCtrl.Controls.Add(robots);
}
#endregion PAGE HEAD
补充:.NET技术 , ASP.NET