0

Hey guys I have a web application and some times when I publish it to the server it drives me crazy cause it's not loading. It returns an error of System.NullReferenceException: Object reference not set to an instance of an object and it hits on Global.asax Application_BeginRequest (I tried Application_AuthenticateRequest - same result).

Global.asax Application_BeginRequest code:

    try
    {
        HttpRequest request = HttpContext.Current.Request;
        string header = request.Headers["X-MicrosoftAjax"];
        if(header != "Delta=true") 
        {
            string path = "~/Logs/TrafficAnalytics.txt";
            string txt = "Visit from: " + ClientInfo.GetClientIP() + " at: " + ClientInfo.GetClientLastRoute + ". Traceback below:\r\nIP Address:        " + ClientInfo.GetClientIP() + "\r\nHostname:          " + ClientInfo.GetClientHostname + "\r\nCoordinates:       " + ClientInfo.GetClientCoordinates() + "\r\nLocation:          " + ClientInfo.GetClientLocation()  + "\r\nOS & Browser Info: " + ClientInfo.GetClientBrowserOs + "\r\nBrowser Info:      " + ClientInfo.GetClientBrowserInfo() + "\r\nOperating System:  " + ClientInfo.GetClientOsInfo() + "\r\nISP:               " + ClientInfo.GetClientIsp();
            Logger.Create(path, txt);
        }
    }
    catch (Exception ex)
    {
        HttpContext con = HttpContext.Current;
        HttpException code = ex as HttpException;
        string bos = Request.UserAgent;
        string path = "~/Logs/ErrorReports.txt";
        string txt = "URL: " + con.Request.Url.ToString() + "\r\nError Message:        " + ex.Message + "\r\nAnalyzed Message:     " + ex.ToString() + "\r\nError Code:           " + code.GetHttpCode().ToString() + "\r\nIpAddressTraceResult: " + ClientInfo.GetClientIP() + "\r\nOS & Browser Info:    " + bos;
        Logger.Create(path, txt);
    }

Logger.cs source:

using System;
using System.IO;
using System.Web;

public class Logger
{
    public static void Create(string p, string t)
    {
        FileInfo path = new FileInfo(HttpContext.Current.Server.MapPath(p));
        string txt = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ffffff") + " :::::::::: " + t + "\r\n------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------";
        path.IsReadOnly = false;
        StreamWriter sw = new StreamWriter(path.ToString(), true);
        sw.WriteLine(txt);
        sw.Flush();
        sw.Close();
    }
}

The error is happening only sometimes it usualy works so I don't know what am I doing wrong. Thanks for all responses...

Dev
  • 677
  • 1
  • 8
  • 19

1 Answers1

0

Well, the best answer is to debug or gather a full stack trace to find which specific line is throwing.

One obvious issue - what if the exception you're catching isn't an HttpException? For example, what if your logging code had an exception in StreamWriter's constructor? It can throw seven different exceptions, none of which are HttpExceptions.

You'd end up in the catch, the value of code would be null because the exception couldn't be cast to an HttpException, and code.GetHttpCode() would throw a NullReferenceException.

PMV
  • 2,058
  • 1
  • 10
  • 15
  • I think I got what you are saying I was oftenly debugging my app and the problem was that sometimes I had this problem on debugging but not on server and sometimes reverse. But I was always stucking at the parts where VS was red-coloring HttpException, null and stuff like that at the debugging console but I never knew what to google for this. I now removed the `code` (it was just to get the Http code 404, 500 etc...) and I believe it will work now. Thank you so much for your reply :) – Dev Oct 24 '16 at 03:35