Question

Hi I have the following code that sometimes works and sometimes it does not:

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
        .............

When I run the above code sometimes I get the following error:

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)
...

Line 60 is the following:

aWriter.RenderEndTag(); // Tr

I am getting nuts with this because if it crashes, you press refresh and it works. For some reason, aWriter object sets to Null but I cannot find why and it looks somehow random even though it always points to the same line. 80% of the times it works correctly and 20% of the times it crashes but without any pattern.

Any idea on what could be happening?

Thanks!

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top