-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; }
    }
}
10
  • 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? Commented Sep 19, 2016 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? Commented Sep 19, 2016 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<Person> { ... } Commented Sep 19, 2016 at 11:04
  • 2
    @CodeCaster I literally just compiled it. It throws an exception on runtime. It compiles fine. Commented Sep 19, 2016 at 11:07
  • 1
    I answered a similar question once: stackoverflow.com/questions/38369967 Maybe THAT should have been be the one marked as duplicate? Commented Sep 19, 2016 at 11:11

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

-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" }}
            }
        }
        };

1 Comment

"Try this" is not a good answer. Explain what you changed and why.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.