c#WebControl中的HtmlTextWriter对象的NullReferenceException

时间:2014-02-28 12:39:15

标签: c# nullreferenceexception web-controls

您好我有以下代码,有时可以使用,有时不会:

protected override void Render(HtmlTextWriter aWriter)
    {
        // Div
        aWriter.RenderBeginTag(HtmlTextWriterTag.Div);

        // Table
        aWriter.AddAttribute(HtmlTextWriterAttribute.Cellspacing, "0");
        aWriter.RenderBeginTag(HtmlTextWriterTag.Table);
        aWriter.RenderBeginTag(HtmlTextWriterTag.Tbody);

        // Header
        aWriter.RenderBeginTag(HtmlTextWriterTag.Tr);

        aWriter.AddAttribute(HtmlTextWriterAttribute.Class, "Header1");
        aWriter.RenderBeginTag(HtmlTextWriterTag.Td);
        aWriter.Write(GetText(24));
        aWriter.RenderEndTag();

        aWriter.AddAttribute(HtmlTextWriterAttribute.Class, "Header2");
        aWriter.RenderBeginTag(HtmlTextWriterTag.Td);
        aWriter.Write(GetText(25));
        aWriter.RenderEndTag();

        aWriter.RenderEndTag(); // Tr
        .............

当我运行上面的代码时,有时会出现以下错误:

Exception type: NullReferenceException 
Exception message: Object reference not set to an instance of an object. 
at Controls.TestControl.Render(HtmlTextWriter aWriter) in C:\Test\SourceCode\Controls\TestControl.cs:line 60
at System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter)
at System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter)
at System.Web.UI.Control.RenderControl(HtmlTextWriter writer)
...

第60行如下:

aWriter.RenderEndTag(); // Tr

我对此感到疯狂,因为如果它崩溃了,你按下刷新它就可以了。 由于某种原因,aWriter对象设置为Null但我找不到原因,即使它始终指向同一行,它看起来也是随机的。 80%的时间它正常工作,20%的时间崩溃但没有任何模式。

对可能发生的事情有任何想法?

谢谢!

0 个答案:

没有答案