Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

193
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda