Solucionado (ver solução)
Solucionado
(ver solução)
2
respostas

Erro ao fazer o PUT dos filmes

Na hora de enviar o verbo PUT apresenta esse erro: System.NullReferenceException: Object reference not set to an instance of an object. at FilmesApi.Controllers.FilmeController.AtualizaFilme(Int32 id, UpdateFilmeDto filmeDto) in C:\Users\DASCO\source\repos\Api\FilmesApi\FilmesApi\FilmesApi\Controllers\FilmeController.cs:line 52 at lambda_method2(Closure , Object , Object[] ) at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.SyncActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeActionMethodAsync() at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeNextActionFilterAsync() --- End of stack trace from previous location --- at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeInnerFilterAsync() --- End of stack trace from previous location --- at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Awaited|20_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope) at Microsoft.AspNetCore.Routing.EndpointMiddleware.g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger) at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.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.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

CONTROLLERusing AutoMapper; using FilmesApi.Data; using FilmesApi.Data.Dtos; using FilmesApi.Models; using Microsoft.AspNetCore.Mvc;

namespace FilmesApi.Controllers;

[ApiController] [Route("[controller]")] public class FilmeController : ControllerBase { private FilmeContext _context; private IMapper _mapper;

public FilmeController(FilmeContext context)
{
    _context = context;
}

[HttpPost]
public IActionResult AdicionaFilme([FromBody] CreateFilmeDto filmeDto)
{
    Filme filme = _mapper.Map<Filme>(filmeDto);
    _context.Filmes.Add(filme);
    _context.SaveChanges();
    return CreatedAtAction(nameof(RecuperaFilmePorId), new {id = filme.Id}, filme);
}

[HttpGet]
public IEnumerable<Filme> RecuperaFilmes([FromQuery] int skip,
    [FromQuery] int take = 10) 
{
    return _context.Filmes.Skip(skip).Take(take);
}

[HttpGet("{id}")]
public IActionResult RecuperaFilmePorId(int id)
{ 
    var filme = _context.Filmes.FirstOrDefault(filme => filme.Id == id);
    if(filme == null) return NotFound();
    return Ok();
}

[HttpPut("{id}")]
public IActionResult AtualizaFilme(int id,
    [FromBody] UpdateFilmeDto filmeDto)
{
    var filme = _context.Filmes.FirstOrDefault(
        filme => filme.Id == id);
    if (filme == null) return NotFound();
    _mapper.Map(filme, filmeDto);      // linha 52
    _context.SaveChanges();
    return NoContent();
}

}

2 respostas
solução!

`

`Problema resolvido public FilmeController(FilmeContext context, IMapper mapper) { _context = context; _mapper = mapper; }

Bom dia Danielle,

Muito obrigado por compartilhar.

Abraços e bons estudos 😄


Caso este post tenha lhe ajudado, por favor, marcar como solucionado ✓. Bons Estudos!