Search code examples
c#oopabstract-classnullreferenceexception

Creating an instance of Abstract Class in a different class


Im trying to create an instance of an abstract class (Employee)

public abstract class Employee
{
    public Employee() { }

    public static SchoolEmployeesContext ctx = new SchoolEmployeesContext();

    public int EmployeeId { get; set; }
    public string EmployeeName { get; set; }
    public string EmployeeSurname { get; set; }
    public string Address { get; set; }
    public int Grade { get; set; }
    public double Salary { get; set; }
    public DateTime DateOfCommencement { get; set; }
    public string Username { get; set; }
    public string Password { get; set; }

    public Department DepartmentOfEmployee { get; set; }

}

Now im trying to fetch an Employee from a context, for multiple functionalities, such as editing and removing an employee. Declaring that the Employee is null in the Method has allowed to remove the "Unassigned" error. But whilst running this is obviously giving me a "null reference exception"

Method to Find Employee Below:

 public Employee Search(int EmpId)
        {
            var Emp = from emp in ctx.Employees
                      where emp.EmployeeId == EmpId
                      select emp;

            Employee Found = null;

            foreach (Employee e in Emp)
            {
               Found.EmployeeName =  e.EmployeeName;
               Found.EmployeeSurname = e.EmployeeSurname;
               Found.Address = e.Address;
               Found.Grade = e.Grade;
               Found.Salary = e.Salary;
               Found.DateOfCommencement = e.DateOfCommencement;
               Found.Username = e.Username;
               Found.Password = e.Password;
               Found.DepartmentOfEmployee = e.DepartmentOfEmployee;
            }


            return Found;
        }

Solution

  • Abstract classes cannot be instantiated.
    They can be inherited from, and the inherited type can be instantiated, but not instantiated directly.

    Looking at your code, this is (I think) what you want to do:

    Remove abstract from your class definition

    Also, you have a completely unused static within your Employee class -

    public static SchoolEmployeesContext ctx = new SchoolEmployeesContext();
    

    So I'd suggest removing that.

    Your Employee class then becomes:

    public class Employee
    {
        public Employee() { }
    
        public int EmployeeId { get; set; }
        public string EmployeeName { get; set; }
        public string EmployeeSurname { get; set; }
        public string Address { get; set; }
        public int Grade { get; set; }
        public double Salary { get; set; }
        public DateTime DateOfCommencement { get; set; }
        public string Username { get; set; }
        public string Password { get; set; }
    
        public Department DepartmentOfEmployee { get; set; }
    }
    

    Also, your Search method looks a little suspect.

    You're selecting many Employees from the database, where EmployeeId is equal to the supplied EmpId
    Doesn't look right to me.

    You can use LINQs SingleOrDefault to retrieve the single item by ID (or null, if it doesn't exist)

    So your Search method would now look like

    public Employee Search(int EmpId)
    {
      //this will return the single employee
      var Emp = ctx.Employees.SingleOrDefault(x=> x.EmployeeId == EmpId);
    
      return Emp;
    }