Skip to content
Sharp

Dependency Injection · interview question

In Spring Boot, what is a potential problem when using field injection with the @Autowired annotation?

Multiple choiceMediumchapter-9-curatedpremiumdependency-injectionconstructor-vs-field-injection
Choose one answer
Answer and explanation

Correct answer

A. It can lead to null pointer exceptions

Why

Field injection with @Autowired can lead to null pointer exceptions because the injection happens after the constructor has executed, leaving the fields uninitialized during construction, which can cause issues if they are accessed prematurely.

Test yourself on Dependency Injection

A free test with instant, explained results. No account needed.

Start free test
All Dependency Injection questions →