locked
Looping function is returning null RRS feed

  • Question

  • I am trying to the function to display desired output but it keeps returning null.  I am little unsure where I may be going wrong.  Any further advice would be very helpful.  Thank you. 

          public  string articleXMLTest()
            {
                string a = testtitle();
                string b = testStory();
                string c = "";
                string results = "";
    
                string[] aA = a.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                string[] bA = b.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                if (aA.Length == bA.Length)
                {
    
                    for (int i = 0; i < aA.Length; i++)
                    {
                        DateTime dt = DateTime.Today;
    
                        XDocument doc = new XDocument(
                   new XDeclaration("1.0", "gb2312", string.Empty),
                   new XElement("article",
                       new XElement("status", "Approved"),
                        new XElement("title", aA[i].ToString()),
                         new XElement("subtitle", aA[i].ToString()),
                          new XElement("synopsis", bA[i].ToString() + "..."),
                           new XElement("url", c),
                            new XElement("display_date", dt.ToShortDateString())
                     ));
                        return results  = results  + Environment.NewLine + doc.ToString();
    
                    }
                  
                }
                //return null;
                return results;
                   
            
            }

    Thursday, June 23, 2016 12:13 PM

Answers

  • The only way this could return null would be if one of the values passed to an XElement constructor were null

    I disagree.  As the code is written, the function articleXMLTest will never return null.  String concatenation (+ operator for strings) treats null as an empty string.

    I simply do not see a code path that can cause it to return null.

    Sure it can return an empty string, but that's different.

    And yes, there's the problem of returning in the middle of the loop rather than just continuing to append to results.  Using StringBuilder is also a cleaner way to build up strings incrementally.

    I can see it throwing an exception -- if testtitle throws an exception, for example.  I can also see if testtitle or testStory returned null, then the split would throw a NullReferenceException.  But throwing an exception is different from returning null.

    So I find the claim that it "keeps returning null" to be dubious.

    • Proposed as answer by Albert_Zhang Friday, June 24, 2016 5:17 AM
    • Marked as answer by DotNet Wang Monday, July 4, 2016 3:11 AM
    Thursday, June 23, 2016 5:32 PM

All replies

  • This works for me.

    The only way this could return null would be if one of the values passed to an XElement constructor were null (although if aA.Length != bA.Length you will get an empty string).

    Have you checked the return values from your testtitle() and testStory() methods? (Though if either of these were null you would be getting a null reference exception from the Split() calls).

    Have you tried stepping through using the debugger?

    Thursday, June 23, 2016 12:27 PM
  • Hi Missy,

    1) Check for the methods what it is returning, the array (aA,bB) length should be equal to enter if condition. example data separated by a comma (A,B,C) to split.

    string a = test title();
    string b = testStory();

    2) I can see here it's not looping for your array length. (maybe you are taking only the 1st matching value, you can decide)

    Because you are breaking the for loop by the keyword return.

    return results  = results  + Environment.NewLine + doc.ToString();
    
    // if you want to loop, then remove return
    
    results  = results  + Environment.NewLine + doc.ToString();


    Thanks, SMSVikasK


    • Edited by SMSVikasK Thursday, June 23, 2016 12:44 PM
    Thursday, June 23, 2016 12:43 PM
  • The only way this could return null would be if one of the values passed to an XElement constructor were null

    I disagree.  As the code is written, the function articleXMLTest will never return null.  String concatenation (+ operator for strings) treats null as an empty string.

    I simply do not see a code path that can cause it to return null.

    Sure it can return an empty string, but that's different.

    And yes, there's the problem of returning in the middle of the loop rather than just continuing to append to results.  Using StringBuilder is also a cleaner way to build up strings incrementally.

    I can see it throwing an exception -- if testtitle throws an exception, for example.  I can also see if testtitle or testStory returned null, then the split would throw a NullReferenceException.  But throwing an exception is different from returning null.

    So I find the claim that it "keeps returning null" to be dubious.

    • Proposed as answer by Albert_Zhang Friday, June 24, 2016 5:17 AM
    • Marked as answer by DotNet Wang Monday, July 4, 2016 3:11 AM
    Thursday, June 23, 2016 5:32 PM