Results 1 to 3 of 3

Thread: [RESOLVED] NullReferenceException when adding to list

  1. #1

    Thread Starter
    Hyperactive Member Cyb3rH4Xter's Avatar
    Join Date
    May 2009
    Location
    Sweden
    Posts
    449

    Resolved [RESOLVED] NullReferenceException when adding to list

    I thought that I knew how to handle those NullReferenceExceptions...but I simply don't know what to do with this one

    Part of my Code:

    Code:
            public class Chapter
            {
                public string Name;
                public TimeSpan Time;
            }
    
            public class ChapterSet
            {
                public List<Chapter> Chapters;
            }
    
            public ArrayList GrabChapters(string searchString)
            {
               
                ...(Unimportant code)...
    
                foreach (XmlNode node in NodeList)
                {
                    ChapterSet chapterSet = new ChapterSet();
    
                    XmlNodeList chapters = node.ChildNodes;
                    foreach (XmlNode chapter in chapters)
                    {
                        Chapter pChapter = new Chapter();
                        pChapter.Name = chapter.Attributes["name"].Value;
                        pChapter.Time = TimeSpan.Parse(chapter.Attributes["time"].Value);
    
                        chapterSet.Chapters.Add(pChapter);
                    }
    
                    Results.Add(chapterSet);
                }
    
                return Results;
            }
    Anyone that can give a good explanation why this happens? (On the red line)
    Last edited by Cyb3rH4Xter; Jun 26th, 2011 at 09:41 AM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: NullReferenceException when adding to list

    The explanation is quite simple: chapterSet.Chapters is null because you have never assigned anything to it. Here's your ChapterSet class:
    Code:
            public class ChapterSet
            {
                public List<Chapter> Chapters;
            }
    It declares a public field named Chapters but it never assigns anything to it internally, so if you expect the field to have a value, you must assign something to it externally. Here's the code that creates and uses a ChapterSet object:
    Code:
                    ChapterSet chapterSet = new ChapterSet();
    
                    XmlNodeList chapters = node.ChildNodes;
                    foreach (XmlNode chapter in chapters)
                    {
                        Chapter pChapter = new Chapter();
                        pChapter.Name = chapter.Attributes["name"].Value;
                        pChapter.Time = TimeSpan.Parse(chapter.Attributes["time"].Value);
    
                        chapterSet.Chapters.Add(pChapter);
    The first line creates the ChapterSet object and the last line tries to use the value of its Chapters field, but nowhere in between do you actually assign anything to that Chapters field. To use your ChapterSet class as is you would have to do this:
    Code:
    chapterSet.Chapters = new List<Chapter>();
    The more sensible approach would be to have the ChapterSet class create the List itself. The most correct approach would be to use a private field and a public read-only property:
    Code:
    public class ChapterSet
    {
        private List<Chapter> _chapters = new List<Chapter>();
    
        public List<Chapter> Chapters { get { return _chapters; } }
    }
    If appropriate, you might add lazy loading:
    Code:
    public class ChapterSet
    {
        private List<Chapter> _chapters;
    
        public List<Chapter> Chapters
        {
            get
            {
                if (_chapters == null)
                {
                    _chapters = new List<Chapter>();
                }
    
                return _chapters;
            }
        }
    }
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Hyperactive Member Cyb3rH4Xter's Avatar
    Join Date
    May 2009
    Location
    Sweden
    Posts
    449

    Re: NullReferenceException when adding to list

    Really good explanation, now I understand! Wish I could give you +rep

    "I wrote I was going to create a bag that I could put things in, then I tried putting the things in the bag before it was created, and that simply doesn't work"

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width