Home > OS >  Running Into a Object Null Error Building Bug Tracker
Running Into a Object Null Error Building Bug Tracker

Time:05-23

I am building an Bug Tracker with Asp.Net following a self paced online course. I'm running into an 'Object not set to a reference error" while attemping to list out Tickets assigned to each project in my Project Details View. I know information isn't being passed in, but I'm not sure how to fix that. Any specific solutions to the problem?

Attempted Work Arounds:

  • Commented out the Ticket Status and Ticket Priority Code and it worked, but it doesn't show everything I'd like.

Youtube Video of Error

Screenshot of Error

Highlighted Code in GitHub Repo

Problem Code:

@model TheBugTrackerProject.Models.Project
@using TheBugTrackerProject.Models.Enums
@using TheBugTrackerProject.Services.Interfaces
@using Microsoft.AspNetCore.Identity
@using TheBugTrackerProject.Models


@inject UserManager<BTUser> userManager
@inject IBTProjectService ProjectService
@inject IBTHistoryService HistoryService

@{
    ViewData["Title"] = "Details";

    BTUser btUser = await userManager.GetUserAsync(User);
    BTUser projectManager = await ProjectService.GetProjectManagerAsync(Model.Id);
}

<h1>Details</h1>

<div>
    <div >
        <div >
            <div >
                <div >
                    <div >
                        <div >
                            @* Project Name *@
                            <h5>@Model.Name</h5>
                            @* Project Description *@
                            <p>@Model.Description</p>
                            <div >
                                <span  style="font-size:small">Project Status</span>
                                <div >
                                    @* Razor code block *@

                                    @{

                                        var start = Model.StartDate.DateTime;
                                        var end = Model.EndDate.DateTime;
                                        var today = DateTime.Now;
                                        var percent = today >= end ? 100 : today < start ? 0 : Math.Round((today.Subtract(start)) / (end.Subtract(start)) * 100);

                                    }

                                    <div  role="progressbar" aria-valuenow="60" aria-valuemin="0" aria-valuemax="100" style="width: @percent;">
                                        @* Use Progress Bar code variable here *@
                                        <span >@percent%</span>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div >
                        <div >
                            <ul >
                                <li>Start Date: <span >@Model.StartDate.ToString("dd MMM, yyyy")</span></li>
                                <li>Deadline: <span >@Model.EndDate.ToString("dd MMM, yyyy")</span></li>
                                <li>Priority: <span >@Model.ProjectPriority?.Name</span></li>
                                @* if() logic *@
                                @if (today < end && today >= start)
                                {
                                    <li>Status:<span >Active</span></li>
                                }
                                else
                                {
                                    <li>Status:<span >Inactive</span></li>
                                }


                            </ul>
                        </div>
                    </div>
                </div>
            </div>
            <div >
                <div >
                    <div >
                        <div >
                            <h2>Project Manager</h2>
                            <hr />
                        </div>
                        <div  style="overflow-y:auto;height:300px;">
                            @* if() logic for Avatar/Default etc *@

                            @if (projectManager != null)
                            {
                                <div>

                                    @if (projectManager.AvatarFileData != null)
                                    {
                                        <img  src="data:image/*;base64, @(Convert.ToBase64String(projectManager.AvatarFileData))" alt="" />
                                    }
                                    else
                                    {
                                        <img  style="width: 60px; height:60px;" src="https://cdn.icon-icons.com/icons2/1378/PNG/512/avatardefault_92824.png" alt="" />
                                    }
                                    <div>
                                        <h5>@projectManager.FullName</h5>
                                        <span>@projectManager.Email</span>
                                        <p >Project Manager</p>
                                        @if (User.IsInRole(nameof(Roles.Admin)) || (btUser.Id == projectManager.Id))
                                        {
                                            <a  style="font:small;" asp-action="AssignUsers" asp-controller="Projects" asp-route-id="@Model.Id">Manage Team</a>
                                        }
                                    </div>

                                </div>
                            }
                            else

                            {
                                <div>
                                    <img  style="width:60px;height:60px;" src="https://cdn.icon-icons.com/icons2/1378/PNG/avatardefault_92824.png" />
                                    <div >
                                        <h5 >Not Assigned</h5>
                                        @if (User.IsInRole(nameof(Roles.Admin)))
                                        {
                                            <span><a asp-action="AssignProjectManager" asp-controller="Home" asp-route-id="@Model.Id" >Assign PM</a></span>
                                        }
                                        <span><a asp-action="AssignProjectManager" asp-controller="Home" asp-route-id="@Model.Id" >Assign PM</a></span>
                                    </div>
                                </div>

                            }
                        </div>
                    </div>
                </div>
            </div>
            <div >
                <div >
                    <div >
                        <div >
                            <h2>Project Team</h2>
                            <hr />
                        </div>
                        <div  style="overflow-y:auto;height:300px;">
                            <ul >
                                @* Logic for avatars *@

                                @foreach (BTUser member in await ProjectService.GetAllProjectMembersExceptPMAsync(Model.Id))
                                {
                                    <li >
                                        <a href="">
                                            <div >
                                                @if (member.AvatarFileData != null)
                                                {
                                                    <img  src="data:image/*;base64,@(Convert.ToBase64String(member.AvatarFileData))" alt="" />
                                                }
                                                else
                                                {
                                                    <img  src="https://cdn.icon-icons.com/icons2/1378/PNG/avatardefault_92824.png" />
                                                }
                                                <div >
                                                    <span >@member.FullName</span>
                                                    <span >@((await userManager.GetRolesAsync(member)).FirstOrDefault())</span>
                                                </div>
                                            </div>
                                        </a>
                                    </li>
                                }
                            </ul>

                        </div>
                    </div>
                </div>
            </div>
            <div >
                <div >
                    <div >
                        <div >
                            <h2>Project Activity</h2>
                            <hr />
                        </div>
                        <div  style="overflow-y:auto;height:300px;background-color:antiquewhite">
                            @* Project Activity loop *@
                            @foreach (TicketHistory history in await HistoryService.GetProjectTicketHistoryAsync(Model.Id, btUser.CompanyId.Value))

                            {
                                <div >
                                    <span >@history.Created.ToString("dd MM, yyyy")</span>
                                    <h6>@history.Description</h6>
                                    <span>By: <a href="" title="@history.User.FullName">@history.User.FullName</a></span>
                                    <div >
                                        <p>The Ticket <b>@history.Property</b>  was edited</p>
                                        <p>@($"Previous {history.Property}:      {history.OldValue}")</p>
                                        <p>@($"Current {history.Property}:      {history.NewValue}")</p>

                                        <div >
                                            <div >
                                                <h6 ></h6>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            }
                        </div>

                    </div>
                </div>
            </div>
            <div >
                <div >
                    <div >
                        <div >
                            <h2>Tickets</h2>
                        </div>
                        <div >
                            <div  style="overflow-y:auto;height:600px;">
                                <table >
                                    <thead >
                                        <tr>
                                            @* Table header *@
                                            <th>Title</th>
                                            <th>Developer</th>
                                            <th>Status</th>
                                            <th>Priority</th>
                                            <th>Date</th>
                                            <th>Action</th>

                                        </tr>
                                    </thead>
                                    <tbody>
                                        @* Table body *@

                                        @foreach (var ticket in Model.Tickets.OrderByDescending(d => d.Created))
                                        {
                                            <tr>
                                                <td><a asp-action="Details" asp-controller="Tickets" asp-route-id="@ticket.Id" style="color:black"><strong>@ticket.Title</strong></a> </td>
                                                <td>
                                                    @if (ticket.DeveloperUserId != null)
                                                    {
                                                        @ticket.DeveloperUser?.FullName
                                                    }
                                                    else
                                                    {
                                                        if (User.IsInRole(nameof(Roles.Admin)) || User.IsInRole(nameof(Roles.ProjectManager)))
                                                        {
                                                            <a  asp-action="AssignDeveloper" asp-controller="Home" asp-route-ticketId="@ticket.Id">Assign Dev</a>
                                                        }

                                                    }
                                                </td>
                                                @if (ticket.TicketStatus.Name == "New")
                                                {
                                                    <td><span >@ticket.TicketStatus.Name</span></td>

                                                }
                                                else
                                                {
                                                    <td><span >@ticket.TicketStatus.Name </span></td>
                                                }
                                                <td><span >@ticket.TicketPriority.Name </span></td>
                                                <td>@ticket.Created.ToString("MM-dd-yyyy")</td>
                                                <td>
                                                    <a  asp-action="Details" asp-controller="Tickets" asp-route-id="@ticket.Id"><i ></i></a>
                                                    @if (ticket.DeveloperUserId == btUser.Id || ticket.OwnerUserId == btUser.Id || (projectManager?.Id == btUser.Id) || User.IsInRole(nameof(Roles.Admin)))
                                                    {
                                                        <a  asp-action="Edit" asp-controller="Tickets" asp-route-id="@ticket.Id"><i ></i></a>
                                                    }

                                                    @if (User.IsInRole("Admin") || (projectManager?.Id == btUser.Id))
                                                    {
                                                        <a ><i ></i></a>
                                                    }
                                                </td>
                                            </tr>
                                        }
                                    </tbody>
                                </table>
                            </div>

                        </div>
                    </div>

                </div>
            </div>
        </div>
    </div>
</div>

Thanks in advance.

P.S. My code is a mess; but looking forward to working with other's on here and possibly in the future.

CodePudding user response:

In your project service, you aren't Include-ing the ticket status so it will be null. Try something like this instead:

Project project = await _context.Projects
    .Include(p => p.Tickets)
        .ThenInclude(t => t.TicketStatus) // <-- add this line
    .Include(p => p.Members)
    .Include(p => p.ProjectPriority)
    .FirstOrDefaultAsync(p => p.Id == projectId && p.CompanyId == companyId);
  • Related