0

I just started learning aboout Api'sand started building a simple one. I built this EmployeeController:

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;

namespace ApiHandsOn1.Controllers
{
    [Route("api/Employee")]
    [ApiController]
    public class EmployeeController : Controller
    {
        List<Employee> _employeeList = new List<Employee>()
            {
                new Employee()
                {
                    Id=1,
                    Name= "abc def",
                    Salary=20000,
                    Permanent= true,
                    //Department= { Id = 1, Name = "Payroll"},
                    Skills ={new Skill{ Id= 1 , Value= "HTML"},new Skill{ Id= 2 , Value="CSS"},new Skill{ Id= 1 , Value= "JS"} },
                    DateOfBirth = new DateTime(01/03/2002)
                },
                new Employee()
                {
                    Id=2,
                    Name= "ghi jkl",
                    Salary=25000,
                    Permanent= false,
                    //Department= { Id = 2, Name = "HR"},
                    Skills ={new Skill{ Id= 1 , Value= "HTML"},new Skill{ Id= 2 , Value="CSS"},new Skill{ Id= 1 , Value= "JS"} },
                    DateOfBirth = new DateTime(15/08/2005)
                }
            };

        public EmployeeController()
        {
            
        }

        private List<Employee> GetStandardEmployeeList()
        {
            
            return _employeeList;
        }
        // GET: EmployeeController
        [HttpGet]
        [Route("GetEmployees")]
        [ProducesResponseType(StatusCodes.Status200OK)]
        public List<Employee> GetStandard()
        {
            List<Employee> empList=GetStandardEmployeeList();
            return empList;
        }

       
    }
}

I have built models for respective classes. But when I hit execute on Swagger , it throws an exception(at _employeeList):

System.NullReferenceException: 'Object reference not set to an instance of an object.'

What am I missing?

11
  • where is the exception thrown? Commented May 3, 2021 at 11:22
  • where I initialise the list _employeeList. Commented May 3, 2021 at 11:23
  • try initializing the list at constructor Commented May 3, 2021 at 11:23
  • @AmirHosseinParsapour I tried that also but it was still giving the same error. Commented May 3, 2021 at 11:24
  • 2
    You don’t initialise DateTime like that; you will be getting a very unexpected value. Commented May 3, 2021 at 11:35

1 Answer 1

1

You need to initialize the Skills property like this:

Skills = new List<Skill>(){new Skill{}, new Skill{}}

and as @smoksnes mentioned, you should put your value between literals in DateTime Initialization

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

Comments

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.