I need some advice on how to deal with null objects being returned and crashing my program. Here is my code:
var employee = _repo.GetEmployeeById(Id); //gets employee by id in ssms
if (employee != null)
{
//do something if an employee exists
return employee;
}
return null; //else return nothing
and then:
try
{
model = await employeeService.GetEmployeeById(Id);
}
catch(NullReferenceException ex)
{
//Do something about it...
}
when an ID which does not exist in the database that gets passed in, the model is null but I handle it in my try and catch a null reference exception but the program still crashes, is there any other way to handle this sort of general error?