Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

189
Visualizações
Is null check of field annotated with javax.inject.Inject required

I have a class where I have injected a field using javax.inject.Inject annotation

import javax.inject.Inject;

public class Foo extends BaseFoo {
  @Inject
  private Bar bar;
  ...

  public void execute(){
    if (bar == null) {
      //log failure message and return
      return;
    }
    //Do your work..
  }
  ...
}

My question is whether in the above example, is null check needed or not? and why?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

The answer is: it depends.

If you think there are valid scenarios, where the injection process will not inject null, then you do need that check. If you think that should never happen, then you probably end up with a NPE at runtime; and then again; you have to figure for yourself if that behavior is "appropriate" in your context (and what/how to deal with it then).

But I would rather strive to make that field final and put the annotation on your constructor; and then you can just put a Objects.requireNonNull() check on the incoming argument; and you are later on sure that any Foo object of are null free.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda