views:

586

answers:

4

I am seeing following exception.

System.NullReferenceException: Object reference not set to an instance of an object. at System.Web.Util.StringUtil.memcpyimpl(Byte* src, Byte* dest, Int32 len) at System.Web.Util.StringUtil.UnsafeStringCopy(String src, Int32 srcIndex, Char[] dest, Int32 destIndex, Int32 len) at System.Web.HttpWriter.Write(String s) at System.Web.HttpResponse.Write(String s)

I already have following checks on the context to which I am responding.

return !(context == null || context.Response == null ||
    context.Response.OutputStream == null ||
    !context.Response.OutputStream.CanWrite);

Can anyone hint on what could be causing this?

A: 

Why even bother with all of those checks? Why not just write out to the output stream and be done with it?

Also, where in the ASP.NET processing pipeline is this call being made?

casperOne
// grab a local copy of the text writer TextWriter writer = context.Response.Output; // write out the message map writer.Write("output data...");
@unknown (google): I mean, where in the processing pipeline. Is this an HTTP handler? An application event handler?
casperOne
yeh http handler
+1  A: 

Are you sure that's where the error is? Looks like the exception started from the Write method of HttpResponse.

Tundey
A: 

I think your error is elsewhere, but I'd suggest writing this expression a little more directly without the negatives:

return context != null && context.Response != null &&
    context.Response.OutputStream != null &&
    context.Response.OutputStream.CanWrite;
recursive
A: 

This might be happening if you are trying to actually write a null object to the buffer IE:

context.Response.OutputStream.Write(null, 0, 0);

Also You could represent it as...

return (context != null && context.Response != null &&
    context.Response.OutputStream != null &&
    context.Response.OutputStream.CanWrite);
Quintin Robinson