How to add another elemant to a nested List<T> in a class.

  • Thread starter Thread starter Rechtfertigung
  • Start date Start date
R

Rechtfertigung

Guest
I am trying to use JSON to model how my classes will look for a Human resource application that I am trying to simulate. The problem is I have a utility class (WorkerCreationUtility) that takes input from the console and simulates creating a work-email and ID number for the user. However, when the method (CreateNewEmployee) to add the worker to the list runs I get an exception thrown "System.NullReferenceException: 'Object reference not set to an instance of an object.'
". I don't know how to fix it and I know its probably something wrong with my code.

BACKGROUND: This is a personal project not a homework assignment and also I am still learning so if you can hint toward the answer and not give it to me flat out that would be nice.

Class containing the outline for what a Manager and Worker object will have:

using System.Collections.Generic;

namespace HumanResources
{

public class JobOffice
{
public List<Staff> Staff { get; set; }
}

public class Staff
{
public List<Manager> Managers { get; set; }
public List<Worker> Workers { get; set; }
}

public class Manager: IStaffModel
{
public string FirstName { get; set; }
public string LastName { get; set; }
public int EmployeeID { get; set; }
public string EmployeeEmail { get; set; }

public Manager(string _FirstName, string _LastName, int _EmployeeID, string _EmployeeEmail)
{
FirstName = _FirstName;
LastName = _LastName;
EmployeeID = _EmployeeID;
EmployeeEmail = _EmployeeEmail;
}
}

public class Worker: IStaffModel
{
public string FirstName { get; set; }
public string LastName { get; set; }
public int EmployeeID { get; set; }
public string EmployeeEmail { get; set; }

public Worker(string _FirstName, string _LastName, int _EmployeeID, string _EmployeeEmail)
{
FirstName = _FirstName;
LastName = _LastName;
EmployeeID = _EmployeeID;
EmployeeEmail = _EmployeeEmail;
}
}

}

Utility class that has functions to add new workers or Managers and adds it to its respective list:

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

namespace HumanResources
{
public abstract class StaffCreationUtility
{

public static void CreateNewEmployee(string FirstName, string LastName)
{
Staff Member = new Staff();
Member.Workers.Add(new Worker(FirstName, LastName, StaffIdNumberGenerator(), StaffEmailGenerator(FirstName, LastName))); //Line where exception is thrown
}

public static void CreateNewManager(string FirstName, string LastName)
{
Manager Manager = new Manager(FirstName, LastName, StaffIdNumberGenerator(), StaffEmailGenerator(FirstName, LastName));
Staff Member = new Staff();
Member.Managers.Add(Manager);
}

public static string StaffEmailGenerator(string FirstName, string LastName)
{
string NewStaffEmail;
Random SelectDomain = new Random();
StringBuilder StaffEmail = new StringBuilder();
StaffEmail.Append(FirstName.Substring(0,3) + LastName.Substring(0,4));
StaffEmail.Append("@" + Enum.GetName(typeof(EmailDomains), SelectDomain.Next(1, 5)));
StaffEmail.Append(".com");
NewStaffEmail = StaffEmail.ToString();
return NewStaffEmail;
}
public static int StaffIdNumberGenerator()
{
Random IdNumberBase = new Random();
return IdNumberBase.Next(945032, 1590907);
}
enum EmailDomains
{
hotmail = 1,
gmail = 2,
yahoo = 3,
icloud = 4,
Microsoft = 5
}
}
}

Interface. The reason why the Manager class and the Employee class have the same property is because I want to eventually add a function that only allows managers to edit other Workers. So the interface assures that no matter what they are allowed to do they all have the same common base of first name, last name, etc...

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

namespace HumanResources
{
interface IStaffModel
{
string FirstName { get; set; }
string LastName { get; set; }
int EmployeeID { get; set; }
string EmployeeEmail { get; set; }
}
}

Continue reading...
 
Back
Top