0

As I iterate over my loop I am getting an when the iteration gets to OrderNo

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

Code:

[HttpGet]
[Authorize]
public async Task<IActionResult> GetWorkSpaceNotifications()
{
   WorkSpaceViewModel ReqWkspace = new WorkSpaceViewModel();

   var reqWorkSpace = _context.Requisition.OrderByDescending(x => x.RequisitionId).ToList();
          
   foreach (var c in reqWorkSpace)
   {
       ReqWkspace.departmentName = _context.Department
                                           .Where(d => d.DepartmentId == c.DepartmentId)
                                           .FirstOrDefault()
                                           .DepartmentName;

       if (c.RequisitionTypeId != null)
       {
           ReqWkspace.requisitionType = _context.RequisitionType
                                                .Where(d => d.RequisitionTypeId == c.RequisitionTypeId)
                                                .FirstOrDefault()
                                                .Description;
       }
            
       ReqWkspace.ReqWorkSpace.RequisitionNo = c.OrderNo;
       ReqWkspace.ReqWorkSpace.QueriesEmail = c.QueriesEmail;
       ReqWkspace.ReqWorkSpace.DateCreated = c.DateCreated;
       ReqWkspace.ReqWorkSpace.Total = c.Total;
       ReqWkspace.Description = c.RequisitionTitle +""+ ReqWkspace.departmentName;
   }

   await Task.Run(() =>
            {
            });

   return View(reqWorkSpace);
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Zidane
  • 1,696
  • 3
  • 21
  • 35

2 Answers2

1

If this line:

ReqWkspace.ReqWorkSpace.RequisitionNo = c.OrderNo

Is throwing that exception, then there are four possible causes:

  • ReqWkspace.ReqWorkSpace is null (seems most likely)
  • c is null (seems unlikely given the query that creates c)
  • The getter for either OrderNo or ReqWorkSpace has code that throws that exception (seems very unlikely)
  • The setter for RequisitionNo has code that throws that exception (seems very unlikely).

There's no way that we can tell which is the problem. I would run it in the debugger, and either break on exception or just view the properties in the first iteration to see if something is not getting initialized.

D Stanley
  • 149,601
  • 11
  • 178
  • 240
0

You should handle these points properlly:

ReqWkspace.departmentName = _context.RequisitionType
                                            .Where(d => d.RequisitionTypeId == c.RequisitionTypeId)
                                            .FirstOrDefault()
                                            .DepartmentName;

If the result of this is null you will get null reference exception so you should defense in that for example:

    var requisitionType = _context.RequisitionType
                                                    .Where(d => d.RequisitionTypeId == c.RequisitionTypeId)
                                                    .FirstOrDefault();
    
ReqWkspace.requisitionType = requisitionType == null ? string.Empty : requisitionType.DepartmentName;

or simply you can do this:

ReqWkspace.requisitionType = _context.RequisitionType
                                                .Where(d => d.RequisitionTypeId == c.RequisitionTypeId)
                                                .FirstOrDefault()?
                                                .DepartmentName;

Same in the description.

Make sure that you initialized the ReqWorkSpace inside the WorkSpaceViewModel constructor.

Ahmed Mansour
  • 91
  • 1
  • 3