Answered by:
Avoiding null exceptions in the generated code

Question
-
User-557230910 posted
Hello,
I am currently learning .Net Core MVC with Entity framework, so I am trying to make an issue tracker app.
I want to have a list of assignees assigned to particular issue, but Person reference in Assignee is null, so I can't access name string.
Below is my UML diagram (I hope there are no errors in there) and code that throws null exception.
// GET: Issues/Details/5 public async Task<IActionResult> Details(int? id) { if (id == null) { return NotFound(); } var issue = await _context.Issues .Include(i => i.Author) .Include(i => i.Project) .Include(i => i.Severity)
// I've added this one line below .Include(i => i.Assignees) .Include(i => i.Status) .FirstOrDefaultAsync(m => m.IssueId == id); // This will throw Person null exception Debug.WriteLine(issue.Assignees.ElementAt(0).Person.FullName); if (issue == null) { return NotFound(); } return View(issue); }What can I do to stop the Debug.Writeline from throwing null exception? Otherwise my view won't show anything.
Thank you for the help
Friday, June 19, 2020 12:50 PM
Answers
-
User-557230910 posted
Thank you for trying to help, I just now managed to find the solution:
var issue = await _context.Issues .Include(i => i.Author) .Include(i => i.Project) .Include(i => i.Severity) .Include(i => i.Status) .Include(i => i.Assignees) //This doesn't work //.Include(i => i.Assignees.Select(p => p.Person)) //But this does! .ThenInclude(p => p.Person) .FirstOrDefaultAsync(m => m.IssueId == id);
Sorry for not being clear enough about my question, I knew I was missing something really elementary.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, June 22, 2020 10:28 AM
All replies
-
User475983607 posted
What can I do to stop the Debug.Writeline from throwing null exception? Otherwise my view won't show anything.Null exception is the most common coding bug in programming. There is no magical solution. It is your responsibility to detect null references before trying to access instance members.
Use the null conditional operator to detect null. Or use a standard "if" statement to check for null before accessing a property of a null type.
Untested example.
Debug.WriteLine(issue.Assignees?.ElementAt?(0)?.Person?.FullName);
Saturday, June 20, 2020 11:26 AM -
User-557230910 posted
Sorry, I must've formulated the question wrong. The code above should work and the variable "issue" should have reference to the Person.
Just like .Include(i => i.Assignees) makes issue.Assignees not null, but I can't find answer to my question in documentation.
Saturday, June 20, 2020 12:47 PM -
User475983607 posted
You have to understand the I can't reproduce this issue and "should" sounds like a guess. Unfortunately, you have not proved any concrete evidence that Person exists. The UML diagram is cut off and there is no navigation property in LINQ query.
Saturday, June 20, 2020 2:09 PM -
User1686398519 posted
Hi Kodeman,
Can you give the model code?
Best Regards,
YihuiSun
Monday, June 22, 2020 10:18 AM -
User-557230910 posted
Thank you for trying to help, I just now managed to find the solution:
var issue = await _context.Issues .Include(i => i.Author) .Include(i => i.Project) .Include(i => i.Severity) .Include(i => i.Status) .Include(i => i.Assignees) //This doesn't work //.Include(i => i.Assignees.Select(p => p.Person)) //But this does! .ThenInclude(p => p.Person) .FirstOrDefaultAsync(m => m.IssueId == id);
Sorry for not being clear enough about my question, I knew I was missing something really elementary.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, June 22, 2020 10:28 AM