1
I have a method in my Pository that searches for a specific user’s last minute and a specific date as well, both passed as argument. However, if there is no record on the last date it returns null and I need it in my controller, if the return is null, it passes the time of the machine as parameter. I’m doing it this way, but it always gives Nullpointerexception.
It’s not possible to work that way?
Alocacaorepository:
@Query("select max(u.horaFim) from Alocacoes u where u.data = :data and u.agendamento.disponivel = :disponiveis")
LocalTime findbyUltimaHora(Date data, Disponiveis disponiveis);
Alocacaoservice:
public LocalTime buscaUltimaHora(Agendamento agendamento) {
Calendar calendar = Calendar.getInstance();
Date data = new Date(calendar.getTime().getTime());
try {
LocalTime hora = alocaoRepository.findByUltimaHora(data, agendamento.getDisponivel());
return hora;
} catch (NoResultException eX) {
System.out.println("Nenhum valor encontrado");
return null;
}
}
Alocacaocontroller:
@PostMapping("/aprovar")
public String aprovarAgendamento(Alocacoes alocacoes, Agendamento agendamento) {
Agendamento agendamentoAlterado = agendamentoService.getAgendamento(agendamento.getIdAgendamento());
agendamentoAlterado.getPedido().setStatus("aprovado");
LocalTime horaInicio = alocacaoService.buscaUltimaHora(agendamentoAlterado);
if (horaInicio == null) {
alocacoes.setHoraInicio(LocalTime.now());
} else {
alocacoes.setHoraInicio(horaInicio);
}
LocalTime horaFim = LocalTime.of(horaInicio.getHour(), 0)
.plusHours(alocacoes.getAgendamento().getPedido().getSugestaoDeHoras());
alocacoes.setHoraFim(horaFim);
agendamentoService.salvarAgendamento(agendamentoAlterado);
alocacaoService.salvarAlocacao(alocacoes);
return "redirect:/homeLider";
}
alocacoes
should be null, you have already tried debugging to find which property is null?– nullptr