2
Answers

how to solve NullReferenceException exception in Log.Error

Sudipta Saha

Sudipta Saha

7y
188
1
My C# code is like this
  1. protected void Application_Error(object sender, EventArgs e)  
  2. {  
  3. var ex = Server.GetLastError();  
  4. Log.Error(ex);  
  5. var httpStatusCode = (ex is HttpException) ? (ex as HttpException).GetHttpCode() : 500;  
  6. var httpContext = ((MvcApplication)sender).Context;  
  7. httpContext.ClearError();  
  8. httpContext.Response.Clear();  
  9. httpContext.Response.StatusCode = httpStatusCode;  
  10. var shouldHandleException = true;  
  11. Exception exceptionToReplace;  
  12. }  
And Log.Error(ex); throws this
 
System.NullReferenceException: Object reference not set to an instance of an object
error cause var ex = Server.GetLastError(); is null .
 
My question is how to solve this error?Any info will be helpful regarding this
Answers (2)
0
Sudipta Saha

Sudipta Saha

NA 41 1.1k 7y
hi sagar thanks for reply :) . i put breakpoint into var ex = Server.GetLastError(); and got ex value is null then continue and found this error in log.error(ex) . I will look into posted link. any other way to solve it . I didn't build any error.aspx page into my application.
0
Sagar  Pandurang Kap

Sagar Pandurang Kap

NA 2.7k 7.5k 7y
Hi,
Firstly start debugging to find where it showing that error.
if not solved visit below link :-
 
https://www.codeproject.com/Questions/145096/how-do-i-solve-the-System-NullReferenceException-e
 
Hope it helps....