Olá,
estou fazendo o projeto acompanhando os videos, ao adicionar o metodo var musicaListResponse = EntityListToResponseList(musicaList);
retorna os seguintes erros no método get de musica:
System.NullReferenceException: Object reference not set to an instance of an object.
at ScreenSound.API.Endpoints.MusicasExtensions.EntityToResponse(Musica musica) in ScreenSound.API/Endpoints/MusicasExtensions.cs:line 109
at ScreenSound.API.Endpoints.MusicasExtensions.<>c.b__3_0(Musica a) in ScreenSound.API/Endpoints/MusicasExtensions.cs:line 104
at System.Linq.Enumerable.SelectListIterator2.Fill(ReadOnlySpan
1 source, Span1 destination, Func
2 func)
at System.Linq.Enumerable.SelectListIterator2.ToList() at ScreenSound.API.Endpoints.MusicasExtensions.EntityListToResponseList(IEnumerable
1 musicaList) in ScreenSound.API/Endpoints/MusicasExtensions.cs:line 104
at ScreenSound.API.Endpoints.MusicasExtensions.<>c.b__0_0(DAL`1 dal) in ScreenSound.API/Endpoints/MusicasExtensions.cs:line 22
at lambda_method7(Closure, Object, HttpContext)
at Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke(HttpContext context)
at Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.Invoke(HttpContext httpContext)
at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider)
at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddlewareImpl.Invoke(HttpContext context)
using Microsoft.AspNetCore.Mvc;
using ScreenSound.API.Requests;
using ScreenSound.API.Response;
using ScreenSound.Banco;
using ScreenSound.Modelos;
using ScreenSound.Shared.Modelos.Modelos;
namespace ScreenSound.API.Endpoints;
public static class MusicasExtensions
{
public static void AddEndPointsMusicas(this WebApplication app)
{
#region Endpoint Músicas
app.MapGet("/Musicas", ([FromServices] DAL<Musica> dal) =>
{
var musicaList = dal.Listar();
if (musicaList is null)
{
return Results.NotFound();
}
var musicaListResponse = EntityListToResponseList(musicaList);
return Results.Ok(musicaListResponse);
});
//Codigo Omitido
private static ICollection<MusicaResponse> EntityListToResponseList(IEnumerable<Musica> musicaList )
{
return musicaList.Select(a => EntityToResponse(a)).ToList();
}
private static MusicaResponse EntityToResponse(Musica musica)
{
return new MusicaResponse(musica.Id, musica.Nome!, musica.Artista!.Id, musica.Artista.Nome);
}
}
** MusicaResponse.cs **
using ScreenSound.API.Requests;
namespace ScreenSound.API.Response;
public record MusicaResponse(int Id, string Nome, int ArtistaId, string NomeArtista);