0

I am sending an email using the smtp. I used the InnerException property within the catch to handle the exception. My problem is I always get an error "Object reference not set to an instance of an object" every time it catches an exception?

 catch 
        {  

    HttpException ErrorType = Server.GetLastError() as HttpException;

                Exception Error = ErrorType;
          //Error line Object reference not set to an instance of an object.
                if (ErrorType.InnerException != null)
                Error = ErrorType.InnerException;
                string ErrorName = Error.GetType().ToString();
                string ErrorMessage = Error.Message;
                string ErrorTrace = Error.StackTrace;

                const string ToAddress = "mail";
                const string FromAddress = "mail";
                const string Subject = "An error has just occurred!";

                // Mail Object

                MailMessage Mail = new MailMessage("mail", "mail");
                Mail.Subject = "Error has ocurred!";
                Mail.IsBodyHtml = true;
                Mail.Priority = MailPriority.High;
                Mail.Body = string.Format(@"

  <html>
      <body>
      <h1>An Error / Exception has just occurred </h1>
     <table cellpadding=""5"" cellspacing=""0"" border=""1"">
     <tr>
      <tdtext-align: right;font-weight: bold"">URL:</td>
      <td>{0}</td>
      </tr>
        <tr>
          <tdtext-align: right;font-weight: bold"">User:</td>
        <td>{1}</td>
       </tr>
      <tr>
    <tdtext-align: right;font-weight: bold"">Exception Type:</td>
     <td>{2}</td>
      </tr>
        <tr> 
          <tdtext-align: right;font-weight: bold"">Message:</td>
        <td>{3}</td>
      </tr>
    <tr>
  <tdtext-align: right;font-weight: bold"">Stack Trace:</td>
  <td>{4}</td>
    </tr> 
      </table>
      </body>
          </html>",
            Request.RawUrl,
            User.Identity.Name,
            ErrorName,
            ErrorMessage,
            ErrorTrace.Replace(Environment.NewLine, "<br />"));
                // Death Screen
                string Markup = ErrorType.GetHtmlErrorMessage();
                if (!string.IsNullOrEmpty(Markup))
                {

                    Attachment attach = 
       Attachment.CreateAttachmentFromString(Markup, "attach.htm");
                     Mail.Attachments.Add(attach);

                }

                // Send the exception mail
                SmtpClient smtp = new SmtpClient();
                smtp.Send(Mail);
                //lblMsg.Text = "Error!";
            }

    }
Jeremy W
  • 1,889
  • 6
  • 29
  • 37
Dodi
  • 111
  • 4
  • 14
  • 1
    There is no guarantee that an Exception will have an InnerException. Often they do not. – Crowcoder Jun 09 '16 at 13:42
  • From what I can see `ErrorType` is null which means either `Server.GetLastError()` returned null or it returned an object which can't be cast to `HttpException`. – Dirk Jun 09 '16 at 13:42

1 Answers1

0

What if the LastServerError was not an HTTPException? Calling Server.GetLastError() as HttpException in that case would give you a null.

Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
Timothy Stepanski
  • 1,186
  • 7
  • 21
  • Thank you for the reply, but what do you think the best solution in different scenarios? – Dodi Jun 09 '16 at 13:46
  • Instead of casting Server.GetLastError() as HttpException cast it like Server.GetLastError() as Exception and be done. – Timothy Stepanski Jun 09 '16 at 13:54
  • In the string Markup = Errortype.GetLastError; It gives me an error! – Dodi Jun 09 '16 at 14:07
  • var ErrorType = Server.GetLastError(); var Error = ErrorType; if (ErrorType.InnerException != null) { Error = ErrorType.InnerException; } – Timothy Stepanski Jun 09 '16 at 14:11
  • The solution I provided is agnostic to the subclass of Exception of which are Server.GetLastError() and it's inner exception. Also, what do you mean "return an object"? – Timothy Stepanski Jun 09 '16 at 14:18
  • I have one in error in my solution wihch is at the line string Markup = ErrorType.GetHtmlErrorMessage(); So your solution may work if I don't have an error with the GetHtmlErrorMessage! – Dodi Jun 09 '16 at 14:23
  • change GetHtmlErrorMessage() to Message string Markup = ErrorType.Message; – Timothy Stepanski Jun 09 '16 at 14:25
  • Unfortunately it gave me the same exception on Line 99: if (ErrorType.InnerException != null) - Object reference not set to an instance of an object. – Dodi Jun 09 '16 at 14:27
  • Why don't you check and make sure Server.GetLastError() isn't returning a null.... – Timothy Stepanski Jun 09 '16 at 14:30
  • If I make it ErrorType.InnerException = null, it will give me an error! How do I make sure if it isn't returning a null value? – Dodi Jun 09 '16 at 14:34
  • var ErrorType = Server.GetLastError(); if (ErrorType == null) { return; } var Error = ErrorType; if (ErrorType.InnerException != null) { Error = ErrorType.InnerException; } – Timothy Stepanski Jun 09 '16 at 14:35