未将对象引用设置到对象的实例。说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。源错误:
行 112: finally
行 113: {
行 114: WrirteFile.Flush();
行 115: WrirteFile.Close();
行 116: }源文件: c:\一些资料及文件\新建文件夹\网站\App_Code\Files.cs 行: 114 堆栈跟踪:
[NullReferenceException: 未将对象引用设置到对象的实例。]
Files.UpdateHtmlPage(String[] strNewsHtml, String[] strStartHtml, String[] strEndHtml, String strHtml) in c:\一些资料及文件\新建文件夹\网站\App_Code\Files.cs:114
CreatIndex.Button1_Click(Object sender, EventArgs e) in c:\一些资料及文件\新建文件夹\网站\CreatIndex.aspx.cs:27
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +111
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +110
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565
行 112: finally
行 113: {
行 114: WrirteFile.Flush();
行 115: WrirteFile.Close();
行 116: }源文件: c:\一些资料及文件\新建文件夹\网站\App_Code\Files.cs 行: 114 堆栈跟踪:
[NullReferenceException: 未将对象引用设置到对象的实例。]
Files.UpdateHtmlPage(String[] strNewsHtml, String[] strStartHtml, String[] strEndHtml, String strHtml) in c:\一些资料及文件\新建文件夹\网站\App_Code\Files.cs:114
CreatIndex.Button1_Click(Object sender, EventArgs e) in c:\一些资料及文件\新建文件夹\网站\CreatIndex.aspx.cs:27
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +111
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +110
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565
这个地方有一个值为null。你设个断点看下就知道了..
Files.UpdateHtmlPage(String[] strNewsHtml, String[] strStartHtml, String[] strEndHtml, String strHtml) in c:\一些资料及文件\新建文件夹\网站\App_Code\Files.cs:114
CreatIndex.Button1_Click(Object sender, EventArgs e) in c:\一些资料及文件\新建文件夹\网站\CreatIndex.aspx.cs:27
{
bool Flage = false;
StreamReader ReaderFile = null;
StreamWriter WrirteFile = null;
string FilePath = HttpContext.Current.Server.MapPath(strHtml);
Encoding Code = Encoding.GetEncoding("gb2312");
string strFile = string.Empty;
try
{
ReaderFile = new StreamReader(FilePath, Code);
strFile = ReaderFile.ReadToEnd(); }
catch (Exception ex)
{
throw ex;
}
finally
{
ReaderFile.Close();
}
try
{
int intLengTh = strNewsHtml.Length;
for (int i = 0; i < intLengTh; i++)
{
int intStart = strFile.IndexOf(strStartHtml[i]) + strStartHtml[i].Length;
int intEnd = strFile.IndexOf(strEndHtml[i]);
string strOldHtml = strFile.Substring(intStart,intEnd-intStart);
strFile = strFile.Replace(strOldHtml, strNewsHtml[i]);
}
WrirteFile = new StreamWriter(FilePath, false, Code);
WrirteFile.Write(strFile);
Flage = true;
}
catch (Exception ex)
{
throw ex;
}
finally
{ WrirteFile.Flush();
WrirteFile.Close();
}
return Flage;
}
{
WrirteFile.Flush();
WrirteFile.Close();
}
for (int i = 0; i < intLengTh; i++)
{
int intStart = strFile.IndexOf(strStartHtml[i]) + strStartHtml[i].Length;
int intEnd = strFile.IndexOf(strEndHtml[i]);
string strOldHtml = strFile.Substring(intStart,intEnd-intStart);
strFile = strFile.Replace(strOldHtml, strNewsHtml[i]);
}
有异常出现,导致
WrirteFile = new StreamWriter(FilePath, false, Code);
没有执行,再往下
finally
{
WrirteFile.Flush();
WrirteFile.Close();
}
WrirteFile还是空的哦.