2016-08-26 8 views
-1

私は仕事用に書いたヘルプデスクプログラムを改訂しています。チケットを作成する前に電子メールを送信しようとすると、私のデータベースに。null参照私のsendmail()メソッドを呼び出すと例外がスローされる

私のcontext.CreateTicket(ticket)メソッドの前にSendMail(ticket)メソッドを呼び出すと、null refが返されます。私は宣言し、手前のチケットオブジェクトを初期化しますが、例外です。

ここでsendmailの私のNewTicket方法

private void CreateNewTicket() 
    { 
     //set search filter to currentuser 
     dS.Filter = "(&(objectClass=user)(anr=" + userName + "))"; 

     //find current user in the acrive director 
     SearchResult sR = dS.FindOne(); 

     var ticket = new HelpTicket 
     { 
      Title = title, 
      DescText = descText, 
      Employee = GetProp(sR, "Name"), 
      EmpEmail = GetProp(sR, "mail"), 
      DateSubmited = DateTime.Now, 
      // Urgency = selectedUrgency, 
      UrgentID = SelectedUrgency.UrgentID, 
      TypeID = SelectedProblemType.TypeID 

     }; 

     try 
     { 
      //if sendmail here it thorws the exception 
      //SendMail(ticket); 
      try { 
       context.CreateTicket(ticket); 
       //If I call context.createticket first it works 
       SendMail(ticket); 
       CloseDialog = true; 
      } 
      catch 
      (System.Exception ex) 
      { 
       MessageBox.Show("Error Submitting ticket: " + ex.Message,"Error Submitting Ticket",MessageBoxButton.OK,MessageBoxImage.Error); 
      } 

     } 
     catch (Exception ex1) 
     { 
      MessageBox.Show("Error Submitting ticket, please try again.\n" + ex1.Message,"Error Creating ticket",MessageBoxButton.OK,MessageBoxImage.Error); 
      CloseDialog = true; 
     } 

    } 

コード(チケット)は、任意のより多くのコードスニペットが必要な場合は

public override void SendMail(HelpTicket ticket) 
    { 
     Microsoft.Office.Interop.Outlook.Application outlook = new Microsoft.Office.Interop.Outlook.Application(); 

     Microsoft.Office.Interop.Outlook.MailItem mailMsg = 
     (Microsoft.Office.Interop.Outlook.MailItem)outlook.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem); 
     Microsoft.Office.Interop.Outlook.Inspector oInspector = mailMsg.GetInspector; 
     mailMsg.To = "[email protected]"; 
     mailMsg.Subject = ticket.Title; 
     mailMsg.HTMLBody = "<b>Urgency: </b>" + ticket.Urgency.Description + "<br/>" + 
      "<b>Problem Type: </b>" + ticket.ProblemType.ProblemDesc + "<br/><hr/><br/>" + 
       ConvertToHtml(ticket.DescText); 
     mailMsg.Send(); 

    } 

、私が知っていると私はそれを投稿します

ための私のコードです

答えて

0

SendMailでは、チケットのオブジェクトプロパティを参照していますが、 は作成していないか、表示していません。とにかくあなたが を投稿したコードに基づいて、ticket.ProblemType.ProblemDescとticket.Urgency.Description を参照すると、参照例外が発生します。

+0

新しいチケットをデータベースに送り返すCreateTicket()メソッドに入る前に、呼び出しに戻ったチケットは他のオブジェクト、UrgencyとProblemTypeへの正しいリンクを持っているので、意味があります。 私はこれを修正する方法を知っていると思います。脳のバンプありがとう! – PnkFld7892

関連する問題