2009-02-19 26 views
1

我看到下面的異常。未將對象引用設置爲對象的實例

System.NullReferenceException:未將對象引用設置爲對象的實例。 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) 在System.Web.HttpWriter.Write(字符串或多個) 在System.Web.HttpResponse.Write(字符串或多個)

我已經在上下文以下檢查到我響應。

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

任何人都可以提示什麼可能會導致此?

回答

0

爲什麼還要打擾所有這些檢查?爲什麼不直接寫出輸出流並使用它呢?

此外,ASP.NET處理管道中的哪個位置正在進行此調用?

+0

//獲取文本書寫器的本地副本 \t \t \t TextWriter writer = context.Response.Output; \t \t \t \t \t \t //寫出消息映射 \t \t \t writer.Write( 「輸出數據...」); – user68575 2009-02-19 19:40:21

1

你確定這是錯誤的地方嗎?看起來像是從HttpResponse的Write方法開始的異常。

0

,我認爲你的錯誤是在其他地方,但我建議寫這個表情多了幾分的情況下直接底片:

return context != null && context.Response != null && 
    context.Response.OutputStream != null && 
    context.Response.OutputStream.CanWrite; 
0

這可能,如果你想真正寫一個空對象到是發生緩衝IE:

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

你也可以把它表示...

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

我在使用異步處理程序和模塊時發生此錯誤。我的問題是我會使用HttpHandler's EndProcessRequest方法中的ThreadPool.QueueUserWorkItem,並在工作項回調中嘗試使用HttpContext。問題是一旦EndProcessRequest返回給調用者(但工作項回調尚未調用),HttpContext對象實例不再是「我的」,並且ASP.NET已經採取了控制並正在將其拆除。有關更多信息,請參見http://odetocode.com/articles/112.aspx

相關問題