0

Here create an API to get the records, in my entity relation table there are twice start date and end date. Here my compulsion is one of them need to keep Null able type.

Here is ER that is SchoolCourses:

 public class SchoolCourses
{
    public Guid ID { get; set; }

    public DateTime StartCourseDate { get; set; } 
    public DateTime EndCourseDate { get; set; } 

    public DateTime? StartSemDate { get; set; }  // Null able type
    public DateTime? EndSemDate { get; set; }  // Null able type

}

I creates a repository for getting the value:

public async Task<ICollection<SchoolCourses>> GetcourseBySchoolId(Guid SchoolId)
    {
        List<SchoolCourses> schoolCourses = null;

            schoolCourses = await _GpsContext.SchoolCourses.AsNoTracking()
            .Where(x => x.SchoolsID == SchoolId)
            .ToListAsync();
        return schoolCourses;
    }

And the Controller are like this:

    public async Task<IActionResult> GetforSchoolCourse(string SchoolId)
    {
        var result = await _schoolCoursesRepository.GetcourseBySchoolId(Guid.Parse(SchoolId));
        List<GetSchoolCourseBySchoolIdVm> getSchoolCourseBySchoolIdVms = new List<GetSchoolCourseBySchoolIdVm>();

        foreach (SchoolCourses schoolCourse in result)
        {
            getSchoolCourseBySchoolIdVms.Add(new GetSchoolCourseBySchoolIdVm
            {
                id = schoolCourse.ID.ToString(),
                StarCoursetDate = schoolCourse.StartCourseDate.ToString(),
                EndCourseDate = schoolCourse.EndCourseDate.ToString(),
                StartSemDate = schoolCourse.StartSemDate.ToString(),
                EndSemDate = schoolCourse.EndSemDate.ToString(),

            });

        }
        return Ok(getSchoolCourseBySchoolIdVms);
    }

Here is View Model for reference:

public class GetSchoolCourseBySchoolIdVm
{
    public string id { get; set; }

    public string StarCoursetDate { get; set; } 
    public string EndCourseDate { get; set; } 

    public string StartSemDate { get; set; } 
    public string EndSemDate { get; set; } 

}

After doing all the above staff it is getting exception error in swagger is following:

System.NullReferenceException: Object reference not set to an instance of an object.; 
Vipin Jha
  • 125
  • 1
  • 1
  • 13
  • Does this answer your question? [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – nvoigt Feb 18 '20 at 12:59
  • Actually this type of problem occurring only when DateTime will null, I could not be able to find the solution for it, if I remove DateTime column it will be start working. – Vipin Jha Feb 18 '20 at 13:08

1 Answers1

0

In your SchoolCourses model StartSemDate and EndSemDate are nullable types, so it must be possible that values of those fields are null. That should have been checked before using it, unlike you have used

StartSemDate = schoolCourse.StartSemDate.ToString(),
EndSemDate = schoolCourse.EndSemDate.ToString(),

here if any of the date is null then calling .ToString() method on it will throw NullReferenceException. Use safe navigation operator to check

schoolCourse.StartSemDate?.ToString()

or

schoolCourse.StartSemDate != null ? schoolCourse.StartSemDate.ToString() : string.Empty
Mukesh Modhvadiya
  • 2,088
  • 2
  • 21
  • 31