Reputation: 24572
I have the following code:
try
{
var result =
from entry in feed.Descendants(a + "entry")
let content = entry.Element(a + "content")
let properties = content.Element(m + "properties")
let notes = properties.Element(d + "DetailsJSON")
let questionMessage = properties.Element(d + "QuestionText")
let title = properties.Element(d + "Title")
let partitionKey = properties.Element(d + "PartitionKey")
where partitionKey.Value == "0001I" && title != null
select new Question
{
Notes = notes.Value ?? "n/a",
Title = title.Value,
QuestionMessage = questionMessage.Value
};
// xx
IList<Question> resultx = null;
foreach (var question in result)
{
resultx.Add(question);
}
// yy
return result;
}
catch (Exception ex)
{
throw new InvalidOperationException("GetQuestions.Get problem", ex);
};
If I comment out the part of the code between xx and yy then it works. Otherwise I get an exception saying:
ex {"Object reference not set to an instance of an object."} System.Exception {System.NullReferenceException}
Can someone give me some ideas about what I might be doing wrong?
Upvotes: 0
Views: 58
Reputation: 7413
Your resultx
is explicitly set to null, so upon resultx.Add()
you get a NullReferenceException
. You need to initialize the list first.
Upvotes: 1
Reputation: 65079
Your list is null
, hence the NullReferenceException
:
IList<Question> resultx = null;
foreach (var question in result)
{
resultx.Add(question); // Ouch. resultx is set to null above here
}
You need to initialize the list to actually be a List<Question>
, not null
:
IList<Question resultx = new List<Question>();
// .. the rest of the code
Upvotes: 3