Null return for a JPA method

Asked

Viewed 248 times

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?

2 answers

1

I ended up solving it this way:

public LocalTime buscaUltimaHoraFimDeAlocacaoDoConsultor(Agendamento agendamento) {
    Calendar calendar = Calendar.getInstance();
    Date data = new Date(calendar.getTime().getTime());
    LocalTime ultimaHora = alocacaoRepository.findbyUltimaHora(data, agendamento.getDisponivel().getIdDisponivel());

    if (ultimaHora == null || ultimaHora.isBefore(LocalTime.now())) {
        int min = LocalTime.now().getMinute();
        if (min >= 0 && min < 30) {
            return LocalTime.of(LocalTime.now().getHour(), 30);
        }
        return LocalTime.of(LocalTime.now().getHour(), 00).plusHours(1);
    }
    return ultimaHora;
}

So it does not return null in case it does not bring anything from the bank.

1

The problem is that in this stretch (Alocacaocontroller):

LocalTime horaFim = LocalTime.of(horaInicio.getHour(), 0)
        .plusHours(alocacoes.getAgendamento().getPedido().getSugestaoDeHoras());

If your timeInicio is null you are calling the getHour() method a null object. Try to catch the timeInicio of your object allocates this way:

    LocalTime horaFim = LocalTime.of(alocacoes.getHoraInicio().getHour(), 0)
        .plusHours(alocacoes.getAgendamento().getPedido().getSugestaoDeHoras());

With that your problem will be solved.

(ps. It would be interesting to have pointed out the stretch that is giving Nullpointer)

Hug.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.