2
Answers

how to solve NullReferenceException exception in Log.Error

Photo of 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)

1
Photo of Ravi Sangtani
NA 806 6k 7y
Hi! try this one
  1. bool isfound = false;  
  2. for (int i = 0; i < allstd.Length; i++)  
  3. {  
  4. isfound = false;  
  5. for (int j = 0; j < present.Length; j++)  
  6. {  
  7. if (allstd[i] == present[j])  
  8. {  
  9. isfound = true;  
  10. break;  
  11. }  
  12. }  
  13. if (!isfound)  
  14. {  
  15. MessageBox.Show(allstd[i]);  
  16. }  
  17. }