-4

There a method called ProccesXmlResponse and there is line written below.

var myMessages = messages.OrderByDescending(x => x.MessageSentDate).ToList();

MessageSentDate is type of DateTime. and I am initializing the messages object for sure.although I got the below exception.Please help

Type : System.NullReferenceException, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
Message : Object reference not set to an instance of an object.
Source : PHOnline.Application.Service
Help link : 
Data : System.Collections.ListDictionaryInternal
TargetSite : System.DateTime <ProccesXmlResponse>b__6(PHOnline.Application.Model.DTO.Vitality.MyMessage)
HResult : -2147467261
Stack Trace :    at PHOnline.Application.Service.Implementations.Vitality.MyMessagesResponseMapper.<ProccesXmlResponse>b__6(MyMessage x)
   at System.Linq.EnumerableSorter`2.ComputeKeys(TElement[] elements, Int32 count)
   at System.Linq.EnumerableSorter`1.Sort(TElement[] elements, Int32 count)
   at System.Linq.OrderedEnumerable`1.<GetEnumerator>d__0.MoveNext()
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at PHOnline.Application.Service.Implementations.Vitality.MyMessagesResponseMapper.ProccesXmlResponse(String response)

Model class: public class MyMessage { /// /// Gets or sets EntityNumber. /// public string MessageID { get; set; }

    /// <summary>
    ///     Gets or sets EntityNumber.
    /// </summary>
    public string MessageSubject { get; set; }

    /// <summary>
    ///     Gets or sets EntityNumber.
    /// </summary>
    public DateTime MessageSentDate { get; set; }

    /// <summary>
    ///     Gets or sets EntityNumber.
    /// </summary>
    public string Read { get; set; }

    /// <summary>
    ///     Gets or sets EntityNumber.
    /// </summary>
    public string FileName { get; set; }

    /// <summary>
    ///     Gets or sets EntityNumber.
    /// </summary>
    public string MIMEType { get; set; }

    /// <summary>
    ///     Gets or sets EntityNumber.
    /// </summary>
    public string AttachmentContent { get; set; }
}
Muralikrishna
  • 149
  • 1
  • 10
  • 2
    I am guessing `messages` is null. – Matthijs Aug 08 '14 at 13:41
  • 3
    Your messages object or one of the messages inside of it is null. – nsgocev Aug 08 '14 at 13:42
  • @Matthijs It's not `messages`. LINQ extensions throw an `ArgumentNullException`, not a `NullReferenceException`, when the source is null. Probably one of the items inside the list. – ahruss Aug 08 '14 at 13:48
  • @ahruss: Makes sense, seeing as he is trying to get a propertyvalue from each of the objects inside messages. – Matthijs Aug 08 '14 at 13:49
  • possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Andrew Whitaker Aug 08 '14 at 13:58
  • var messages = new List(); var et = messages.OrderByDescending(x => x.MessageSentDate); I tried this code,as you can see the items inside the list of messages are null.. I am not receiving any exception as i am getting above.The exception which I mentioned earlier I got it from Pre Production environment. – Muralikrishna Aug 18 '14 at 13:21

1 Answers1

0

You have 2 objects, your messages, which I am assuming is some kind of list/array which you have initialized and inside the list are message objects. At least one of the message objects inside the list is null so when it does x.MessageSentDate there is no .MessageSentDate. Thus throwing a NullException.

bowlturner
  • 1,817
  • 3
  • 19
  • 33