-2

Currently doing some homework on C# basics. I'm getting a null pointer on this line and can't figure out what's caussing it.:

List<Project> projecten = new List<Project>{
    new Project {
        name = "project a",
        deelnemers =
        {
            new Person { name = "Ed" },
            new Person { name = "Mike" },
        }
    },
    new Project {
        name = "project b",
        deelnemers = {
            new Person {name = "Max" },
            new Person {name = "Peter" },
        }
    }
};

the person and project classes are defined correctly I think:

public class Project
{
    public string name { get; set; }
    public List<Person> deelnemers {get; set;}
}

public class Person
{
    public string name { set; get; }
}

It's probably something stupid but I don't see it.

The complete file is this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.Generic;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {

            // Maak een collection mbv  initializers van minimaal 3 projecten met meerdere projectleden

            List<Project> projecten = new List<Project>{
                new Project {
                    name = "project a",
                    deelnemers =
                    {
                        new Person { name = "Xanvier" },
                        new Person { name = "Jantje" },
                    }
                },
                new Project {
                    name = "project b",
                    deelnemers = {
                        new Person {name = "Pietje" },
                        new Person {name = "Keesje" },
                    }
                }
            };
            List<Int16> p = new List<Int16> { 1,2,3 };
            //var projectje = new Project{ name = "project a" };

        }
    }

    public class Project
    {
        public string name { get; set; }
        public List<Person> deelnemers {get; set;}
    }

    public class Person
    {
        public string name { get; set; }
    }
}
Ravenix
  • 1,010
  • 4
  • 15
  • 40
  • I don't think the error is where you think it is - can you share a full, runnable (but minimal) example that demonstrates the problem? – Ant P Sep 19 '16 at 11:00
  • @CodeCaster, this is the line where it's caussing (the first code block is one line) Can you clarify why it's not compiling? – Ravenix Sep 19 '16 at 11:03
  • 1
    The duplicate question shouldn't have been marked as such. The OP knows what a NullReferenceException is. `deelnemers = { new Person ... }` **will** compile. The problem is, it does not initialize your list. It will only call `Add`. You need to say `deelnamers = new List { ... }` – Dennis_E Sep 19 '16 at 11:04
  • 2
    @CodeCaster I literally just compiled it. It throws an exception on runtime. It compiles fine. – ThePerplexedOne Sep 19 '16 at 11:07
  • @CodeCaster when I click build all: `Build succeeded`, so it will build. Error comes up on runtime. – Ravenix Sep 19 '16 at 11:07
  • 1
    I answered a similar question once: http://stackoverflow.com/questions/38369967 Maybe THAT should have been be the one marked as duplicate? – Dennis_E Sep 19 '16 at 11:11
  • @Dennis nice find, [this is a better one I guess](http://stackoverflow.com/questions/30620808/why-does-this-nested-object-initializer-throw-a-null-reference-exception), but I can't vote anymore. :) – CodeCaster Sep 19 '16 at 11:13
  • @CodeCaster I didn't know about that one. Nice one too. – Dennis_E Sep 19 '16 at 11:17
  • Found another one: http://stackoverflow.com/questions/4899297 – Dennis_E Sep 19 '16 at 11:28
  • Yeah, the problem was that I didn't knew what went wrong, so couldn't search for a solution ;) – Ravenix Sep 19 '16 at 11:31

3 Answers3

1

This part is a collection initializer:

deelnemers =
    {
        new Person { name = "Ed" },
        new Person { name = "Mike" },
    }

This is perhaps one of the most confusing forms of syntax in C#. It does not initialize your list. It only calls Add on it. You need to initialize the List as well:

deelnemers = new List<Person> {
    new Person { name = "Xanvier"},
    //
}

Or, you can initialize the List in the constructor of Project. That way, your original code will work as expected.

Dennis_E
  • 8,751
  • 23
  • 29
0

The solution is, like Dennis_E said in a comment: deelnemers = { new Person ... } will compile. The problem is, it does not initialize your list. It will only call Add. You need to say:

deelnemers = new List<Person> { ...

So the problem was that I forgot to initialize the list.

Ravenix
  • 1,010
  • 4
  • 15
  • 40
-1

try this List projecten = new List{ new Project { name = "projecta", deelnemers = new List{

                {new Person { name = "Ed" }},
                {new Person { name = "Mike" }}
                }
            },
        new Project {
            name = "projectb",
            deelnemers = new List<Person>{
                {new Person {name = "Max" }},
                {new Person {name = "Peter" }}
            }
        }
        };