0

I try getting all the Xelement value and insert into an array, however when the Xelement is null, it will show the error as below when insert into array:

Object reference not set to an instance of an object.

Is it possible to remove the null value? Or replace the null value with Not null value? Please advice me.

My Node in XML format in Database (3rd value is null):

<AnswerData><Answer1>a1</Answer1><Answer2>a2</Answer2><Answer3></Answer3></AnswerData>

My controller:

int z = 0;
string[] multiresultanswer = new string[choicesubqarray.Count()];

for (int x = 0; x < multiresultanswer.Count(); x++)
{
    XElement RateAns =
        (from node in qconfig.Elements("Answer" + (x + 1))
            select node).SingleOrDefault();

    // Error when store 3rd value ====> multiresultanswer[z++] = RateAns.Value;       
 }
Edward.K
  • 556
  • 3
  • 11
  • 33
  • 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) – Orel Eraki Jul 15 '15 at 08:14
  • Why you need to remove `RateAns`?You can change `RateAns.Remove()` to `continue` – Sky Fang Jul 15 '15 at 08:22

4 Answers4

3

I think it would be easier for you to work this way:

string[] multiresultanswe = Enumerable
    .Range(1, choicesubqarray.Count())
    .Select(x => (from node in qconfig.Elements("Answer" + x)
               select node).SingleOrDefault())
    .Where(x => x != null)
    .Select(x => x.Value)
    .ToArray();

That would entail getting the range of answers (Enumerable.Range), changing the list of numbers to required result (Select), leaving out the nulls (Where), converting it to the value (Select), and putting the results into the array (ToArray).

You can also improve (from node in qconfig.Elements("Answer" + x) select node).SingleOrDefault() by changing it into qconfig.Elements("Answer" + x).SingleOrDefault() which does the same thing.

Jonathan Camilleri
  • 611
  • 1
  • 6
  • 17
2

You seem to misunderstand the meaning of SingleOrDefault.

SingleOrDefault returns null if no elements have been found. Thus, in the case of (RateAns == null), there is no answer with the given number in your XML. Thus, there is nothing to remove.

Heinzi
  • 167,459
  • 57
  • 363
  • 519
1

You have to check if the Value-property of your current XElement is NULL.

var rateAns = qconfig.Elements("Answer" + (x + 1)).Where(el => el.Value != null));

Thus you won´t need any further handling for NULL-values, so your RateAns == null is obsolete because your query does not return any NULL-elements.

MakePeaceGreatAgain
  • 35,491
  • 6
  • 60
  • 111
1
string xml = @"<AnswerData><Answer1>a1</Answer1><Answer2>a2</Answer2><Answer3>null</Answer3></AnswerData>";
XElement root = XElement.Parse(xml);
int count = 5;
var query = from n in Enumerable.Range(1, count)
            join e in root.Elements() on n.ToString() equals e.Name.LocalName.Substring(6) into g
            from e in g.DefaultIfEmpty()
            select e == null ? null : e.Value == "null" ? null : e.Value;
foreach (var q in query)
{
    Console.WriteLine(q == null ? "NULL VALUE" : q);
}

You want to skip where the element's value is null string

Sky Fang
  • 1,101
  • 6
  • 6