-2

This one will be very easy for you pros - but I've now wasted hours on it myself. Here's a simplified version of what I'm trying to do:

(1) I have an 'Employment' class which can store 3 strings [EmployerName], [JobTitle] and [PeriodOfEmployment]. I am successfully able to create an instance of Employment and populate it with example entries.

(2) I want to populate a wrapper class called 'CurriculumVitae' which contains a [PersonName] string plus a List variable containing all Employments ever held by the person. I want to use a loop to add instances of Employment to the instance of CurriculumVitae, so that the CurriculumVitae class ends up holding a person's full job history in its EmploymentsList.

(3) In Visual Studio am getting the error message:

System.NullReferenceException: 'Object reference not set to an instance of an object.'
CurriculumVitae.EmploymentsList.get returned null.'.

My simplified code is:

using System;
using System.Collections.Generic;

namespace CurriculumVitaeExample
{
    class Program
    {
        static void Main(string[] args)
        {
            var CurriculumVitae = new CurriculumVitae();
            CurriculumVitae.Open();
        }
    }
}

My Employment class looks like this:

public class Employment
{
    public string EmployerName { get; set; }
    public string JobTitle { get; set; }
    public string PeriodOfEmployment { get; set; }
}

My CurriculumVitae class tries to make use of the Employment class in a List, like this:

public class CurriculumVitae //(version 1)
{
    public string PersonName { get; set; }
    public List<Employment> EmploymentsList { get; set; }

    // Open method:
    public void Open()
    {
        Employment Employment = new Employment();

        Employment.EmployerName = "McDonalds";
        Employment.JobTitle = "Ice Cream Guru";
        Employment.PeriodOfEmployment = "Jan 2019 - Present";

        this.EmploymentsList.Add(Employment);
    }

}

I also tried adding a constructor for the EmploymentsList within the CurriculumVitae class but it didn't help:

public class CurriculumVitae //(version 2)
{
        // Constructor:
        public CurriculumVitae()
        {
            List<Employment> EmploymentsList = new List<Employment>();
        }
        ...
}
J Brog
  • 7
  • 6
  • The error occurs on the line: this.EmploymentsList.Add(Employment); – J Brog Apr 13 '20 at 15:35
  • 1
    You need to understand the difference in local variables vs a property and how to instantiate a list of an object. In the CurriculumVitae constructor you just need `EmploymentsList = new List();` What you had would have created a new local variable, which won't help. – mason Apr 13 '20 at 15:39

1 Answers1

-1

Change the name of CurriculumVitae variable to curriculumVitae

static void Main(string[] args)
{
    var curriculumVitae = new CurriculumVitae();
    curriculumVitae.Open();
}

and Employment to employment

public void Open()
{
    Employment employment = new Employment();
    employment.EmployerName = "McDonalds";
    employment.JobTitle = "Ice Cream Guru";
    employment.PeriodOfEmployment = "Jan 2019 - Present";

    this.EmploymentsList.Add(employment);
}

And finally you must initialize EmploymentsList like this in version1

public List<Employment> EmploymentsList { get; set; } = new List<Employment>();

or in constructor in version2

public CurriculumVitae()
{
    this.EmploymentsList = new List<Employment>();
}
Farhad Zamani
  • 5,381
  • 2
  • 16
  • 41
  • 3
    Did you read the error message in the question? How does your answer solve that error? (hint: it doesn't) – mason Apr 13 '20 at 15:37
  • @mason Class name's and variable name are same. and `CurriculumVitae.Open();` not working because `Open()` method not static and should create an instance of `CurriculumVitae` – Farhad Zamani Apr 13 '20 at 15:40
  • Thanks for the reply - I do see that it relates to null references but being a beginner I have not been able to translate that insight into a simple fix to the line this.EmploymentList.Add(employment); I was hoping it was a simple syntax error that a pro could spot. – J Brog Apr 13 '20 at 15:41
  • 3
    Again: your solution does *nothing* to address the error message from the question. Did you read the question? – mason Apr 13 '20 at 15:41
  • Farhad - thanks but that didnt work. My view is that I should not be separately instantiating the EmployeeList object as it is already a member of the CurriculumVitae class, which is being instantiated. But I am not sure if I need to instantiate it within the CurriculumVitae constructor - do I? And if so what was I doing wrong in version 2? – J Brog Apr 13 '20 at 15:52
  • @JBrog Your welcome. i updated my answer, check again – Farhad Zamani Apr 13 '20 at 15:54
  • Farhad thank you so much - your syntax for the constructor fixed the problem. For your info, the potential issue regarding capitalisation which made the variable name the same as the class name was not an issue - I have confirmed this by returning to my original naming convention. – J Brog Apr 13 '20 at 16:00
  • @JBrog Good luck ;) – Farhad Zamani Apr 13 '20 at 16:01
  • Farhad I am also new to StackOverflow and thought that I would be able to vote or accept your answer in some way to show my gratitude, but I am not sure how, particularly as someone called Igor closed the question before we had finished our conversation. Apparently I had not done enough 'research effort'. I had spent hours reading up on it but Igor clearly makes no allowance for beginners. As I suspected, the fix was very quick for a pro like yourself and I think this question and your answer will help others who are new to C#. But thank you again. – J Brog Apr 13 '20 at 16:08
  • @JBrog no need to thank. glad i helped you, I did my human duty to help you to solve your problem faster, wish you luck – Farhad Zamani Apr 13 '20 at 16:16
  • @JBrog There should be a check mark next to the post. Click on it to accept it. See [Capitalization Conventions](https://learn.microsoft.com/en-us/dotnet/standard/design-guidelines/capitalization-conventions?redirectedfrom=MSDN) for the standards most programmers use. – LarsTech Apr 13 '20 at 16:19
  • Found it! (the check mark). lol. Told you I was a beginner! Thanks LarsTech – J Brog Apr 13 '20 at 16:22
  • 1
    @JBrog The upvote and downvote controls are there, too. Use the '@' sign in front of a user name in comments to notify that person you are commenting to them. – LarsTech Apr 13 '20 at 16:24