Mistake:
NullReferenceException: Object reference not set to an instance of an object. NullReferenceException: Object reference not set to an instance of an object. DatingApp.API.Controllers.UsersController.GetUsers() in UsersController.cs, line 29 Stack Query Cookies Headers NullReferenceException: Object reference not set to an instance of an object. DatingApp.API.Controllers.UsersController.GetUsers() in UsersController.cs + [HttpGet] public async Task<IActionResult> GetUsers() { var users = await _repo.GetUsers(); var usersToReturn = _mapper.Map<IEnumerable<UserForListDto>>(users); return Ok(usersToReturn); } [HttpGet("{id}")] public async Task<IActionResult> GetUser(int id) Microsoft.AspNetCore.Mvc.Internal.ActionMethodExecutor+TaskOfIActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, object controller, object[] arguments) System.Threading.Tasks.ValueTask<TResult>.get_Result() Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeActionMethodAsync() Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeNextActionFilterAsync() Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Rethrow(ActionExecutedContext context) Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted) Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeInnerFilterAsync() Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextResourceFilter() Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext context) Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted) Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeFilterPipelineAsync() Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeAsync() Microsoft.AspNetCore.Routing.EndpointMiddleware.Invoke(HttpContext httpContext) Microsoft.AspNetCore.Routing.EndpointRoutingMiddleware.Invoke(HttpContext httpContext) Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context) Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)' 'at DatingApp.API.Controllers.UsersController.GetUsers() in H:\Projects\DatingApp.API\Controllers\UsersController.cs:line 29 at Microsoft.AspNetCore.Mvc.Internal.ActionMethodExecutor.TaskOfIActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments) at System.Threading.Tasks.ValueTask'1.get_Result() at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeActionMethodAsync() at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeNextActionFilterAsync() at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Rethrow(ActionExecutedContext context) at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted) at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeInnerFilterAsync() at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextResourceFilter() at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext context) at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted) at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeFilterPipelineAsync() at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeAsync() at Microsoft.AspNetCore.Routing.EndpointMiddleware.Invoke(HttpContext httpContext) at Microsoft.AspNetCore.Routing.EndpointRoutingMiddleware.Invoke(HttpContext httpContext) at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context) at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)
AutoMapperProfiles.cs:
using System.Linq; using AutoMapper; using DatingApp.API.Dtos; using DatingApp.API.Models; namespace DatingApp.API.Helpers { public class AutoMapperProfiles : Profile { public AutoMapperProfiles() { AllowNullCollections = true; CreateMap<User, UserForListDto>() .ForMember(dest => dest.PhotoUrl, opt => { opt.MapFrom(src => src.Photos.FirstOrDefault(p => p.IsMain).Url); }) .ForMember(dest => dest.Age, opt =>{ opt.MapFrom(src => src.DateOfBirth.CalculateAge()); }); CreateMap<User, UserForDetailDto>() .ForMember(dest => dest.PhotoUrl, opt => { opt.MapFrom(src => src.Photos.FirstOrDefault(p => p.IsMain).Url); }) .ForMember(dest => dest.Age, opt =>{ opt.MapFrom(src => src.DateOfBirth.CalculateAge()); }); CreateMap<Photo, PhotosForDetailDto>(); } } }
UserForListDto.cs:
using System; namespace DatingApp.API.Dtos { public class UserForListDto { public int Id { get; set; } public string Username { get; set; } public string Gender { get; set; } public int Age { get; set; } public string KnownAs { get; set; } public DateTime Created { get; set; } public DateTime LastActive { get; set; } public string City { get; set; } public string Country { get; set; } public string PhotoUrl { get; set; } } }
PhotosForDetailDto.cs
using System; namespace DatingApp.API.Dtos { public class PhotosForDetailDto { public int Id { get; set; } public string Url { get; set; } public string Description { get; set; } public DateTime DateAdded { get; set; } public bool IsMain { get; set; } } }
Photo.cs:
using System; namespace DatingApp.API.Models { public class Photo { public int Id { get; set; } public string Url { get; set; } public string Description { get; set; } public DateTime DateAdded { get; set; } public bool IsMain { get; set; } public User User { get; set; } public int UserId { get; set; } } }
User.cs
using System; using System.Collections.Generic; namespace DatingApp.API.Models { public class User { public int Id { get; set; } public string Username { get; set; } public byte[] PasswordHash { get; set; } public byte[] PasswordSalt { get; set; } public string Gender { get; set; } public DateTime DateOfBirth { get; set; } public string KnownAs { get; set; } public DateTime Created { get; set; } public DateTime LastActive { get; set; } public string Introduction { get; set; } public string LookingFor { get; set; } public string Interests { get; set; } public string City { get; set; } public string Country { get; set; } public ICollection<Photo> Photos { get; set; } } }
Extensions.cs:
using System; using Microsoft.AspNetCore.Http; namespace DatingApp.API.Helpers { public static class Extensions { public static void AddApplicationError(this HttpResponse response, string message) { response.Headers.Add("Application-Error", message); response.Headers.Add("Access-Control-Expose-Headers", "Application-Error"); response.Headers.Add("Access-Control-Allow-Origin", "*"); } public static int CalculateAge(this DateTime theDateTime) { var age = DateTime.Today.Year - theDateTime.Year; if (theDateTime.AddYears(age) > DateTime.Today) { age--; } return age; } } }
IDatingRepository.cs:
using System.Collections.Generic; using System.Threading.Tasks; using DatingApp.API.Models; namespace DatingApp.API.Data { public interface IDatingRepository { void Add<T>(T entity) where T : class; void Delete<T>(T entity) where T : class; Task<bool> SaveAll(); Task<IEnumerable<User>> GetUsers(); Task<User> GetUser(int id); } }
DatingRepository.cs:
using System.Collections.Generic; using System.Threading.Tasks; using AutoMapper; using DatingApp.API.Models; using Microsoft.EntityFrameworkCore; namespace DatingApp.API.Data { public class DatingRepository : IDatingRepository { private readonly DataContext _context; private readonly IMapper _mapper; public DatingRepository(DataContext context, IMapper mapper) { _context = context; _mapper = mapper; } public void Add<T>(T entity) where T : class { _context.Add(entity); } public void Delete<T>(T entity) where T : class { _context.Remove(entity); } public async Task<bool> SaveAll() { return await _context.SaveChangesAsync() > 0; } public async Task<IEnumerable<User>> GetUsers() { var users = await _context.Users.Include(p => p.Photos).ToListAsync(); return users; } public async Task<User> GetUser(int id) { var user = await _context.Users.Include(p => p.Photos) .FirstOrDefaultAsync(u => u.Id == id); return user; } } }