Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

195
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!