2017-04-05 9 views
-2

に割り当てている間に、これは何かわかりませんNullRefrenceExceptionです。私はエラーを取得していたいくつかの不確実な状況があります。エラーSystem.NullReferenceExceptionリスト<T>をプロパティC#

いくつかのクラスのプロパティに値を割り当てようとしています。

ここに私のクラスがあります。

// These classes are in EmailProcess namspace 
public class ActionedEmailReport 
{ 
    public Message Message { get; set; } 
    public string SaveToSentItems { get; set; } 
} 

public class ToRecipient 
{ 
    public EmailObject.EmailAddress EmailAddress { get; set; } 
} 

public class Message 
{ 
    public string Subject { get; set; } 
    public Body Body { get; set; } 
    public List<ToRecipient> ToRecipients { get; set; } 
} 

public class Body 
{ 
    public string ContentType { get; set; } 
    public string Content { get; set; } 
} 

// Below class in EmailObject namespace. 
namespace EmailObject 
{ 
    public class EmailAddress 
    { 
     public string Address { get; set; } 
    } 
} 

ここでは、クラスのプロパティに値を割り当てるコードを示します。オブジェクト参照オブジェクトのインスタンスに設定されていない:私は

System.NullReferenceExceptionというエラーを取得していますラインactionedReport.Message.ToRecipients = toRec;

public void EmailProcessing(string recepeint) 
{ 
    ActionedEmailReport actionedReport = new ActionedEmailReport(); 
    List<ToRecipient>toRecipient = new List<ToRecipient>(); 
    EmailObject.EmailAddress emailAddress= new EmailObject.EmailAddress(); 
    emailAddress.Address = recepeint; 
    toRecipient.Add(new ToRecipient() 
{ 
    EmailAddress=emailAddress 
}); 

    // I'm getting error on the below line.  
    actionedReport.Message.ToRecipients = toRecipient; 
    actionedReport.Message.Body.Content = "Hello"; 
    actionedReport.Message.Body.ContentType = "Text"; 
    actionedReport.SaveToSentItems = "True"; 
    actionedReport.Message.Subject = "Demo Email" 
} 

私は入力を正しくチェックして、toRecipientに値を割り当てていることを確認してから、なぜこのエラーが表示されるのですか?これは私を夢中にさせている。

+1

'actionedReport.Message'は割り当てられていないようですが、デバッガで実行するとこれを確認できます。 – Joe

+0

'toRecipient'はnullではないかもしれませんが、' Message'や 'ToRecipients'はどうでしょうか?新しい 'ActionedEmailReport'をビルドすると、それに含まれるオブジェクトはインスタンス化されますか? – pgruber

+0

あなたの複製である質問を確認してください。具体的には、受け入れられた回答の「クラスインスタンス」と「間接」の部分。 –

答えて

1

のプロパティがインスタンス化されていません。 ActionedEmailReportにコンストラクタを追加し、プロパティを新たに追加する必要があります。 クラスのBodyプロパティでも同じ処理を行う必要があります。

+0

クール!このトリックはうまくいった。どうもありがとう。 –

関連する問題