I am trying to get an object from the database and return to React in my c# controller. If I return the object as it comes from the database all works fine, but if I convert to a DTO object I get Object reference not set to an instance of an object
error.
In my controller I have,
[HttpGet("interaction/{id}")]
public async Task<ActionResult<DrugInteractionDTO>> getInteractionDetails(int id)
{
var interaction = await _context.DrugInteractions.FindAsync(id);
if(interaction == null) {
return NotFound();
}
return new DrugInteractionDTO
{
Severity = interaction.Severity,
SeverityAsString = interaction.Severity.ToString(),
ProDetailedInformation = interaction.ProDetailedInformation,
BasicDetailedInformation = interaction.BasicDetailedInformation,
Antifungal = interaction.AntifungalAgent.Name,
InteractingDrug = interaction.InteractingDrug.GenericName,
ID = interaction.ID
};
}
I have used this pattern many times to convert to a DTO before sending back to React without issue. I am not sure why I am getting an issue here.
My DrugInteractionDTO
looks like this,
public class DrugInteractionDTO
{
public string Antifungal { get; set; }
public string InteractingDrug { get; set; }
public string BasicDetailedInformation { get; set; }
public string ProDetailedInformation { get; set; }
public Severity Severity { get; set; }
public string SeverityAsString {get; set; }
public int ID { get; set; }
}