Incorrect @Autowired Usage Causing NullPointerException in Spring Boot (Production Guide)

Seeing a NullPointerException even though you’ve used @Autowired is one of the most common and frustrating problems for Spring Boot developers. It often leads developers to believe that Spring has failed to inject a dependency, when the actual issue lies in how the object was created or configured.

In most cases, the problem is not with Spring itself. Instead, it happens because the class is not managed by the Spring container, component scanning is incorrect, multiple beans exist, or dependency injection is used incorrectly.

In this guide, you’ll learn why @Autowired becomes null, how Spring performs dependency injection internally, common production scenarios that cause this issue, and the best practices to prevent it.

Table of Contents

  • What does @Autowired actually do?
  • Why does @Autowired become null?
  • Common causes of NullPointerException
  • Constructor injection vs field injection
  • Production best practices
  • Frequently Asked Questions

What Does @Autowired Actually Do?

@Autowired tells Spring to automatically inject a matching bean from the Application Context into another Spring-managed bean.

@Service
public class UserService {

    @Autowired
    private EmailService emailService;

}

When Spring creates UserService, it searches for a matching EmailService bean and injects it automatically.

If you’re new to dependency injection and want to understand how Spring resolves dependencies internally, read our complete guide:

Dependency Injection in Spring Boot – Production Grade Explanation

Why Does @Autowired Become Null?

Spring can only inject dependencies into objects that it creates and manages.

If you create an object manually using:

UserService service = new UserService();

Spring has no control over that object.

Therefore,

emailService == null

This is the most common reason developers encounter a NullPointerException.

To understand why this happens, it’s helpful to know how Spring creates beans during application startup.

Read:

How Spring Boot Creates Beans (Bean Lifecycle Simplified)

Common Cause 1 — Creating Objects Using new

Incorrect:

UserService service = new UserService();
service.sendEmail();

Correct:

@Autowired
private UserService userService;

or

ApplicationContext.getBean(UserService.class);

Always allow Spring to create and manage your beans.

Common Cause 2 — Missing Component Annotation

If a class isn’t registered as a Spring bean, dependency injection cannot occur.

Incorrect:

public class EmailService {
}

Correct:

@Service
public class EmailService {
}

or

@Component
public class EmailService {
}

Common Cause 3 — Wrong Package Scanning

Spring Boot scans components starting from the package containing @SpringBootApplication.

If your package structure is incorrect, Spring may never discover your bean.

@SpringBootApplication(scanBasePackages = "com.company")

Understanding the startup process makes package scanning much easier to understand.

Read:

How Spring Boot Application Starts (Startup Flow Explained)

Common Cause 4 — Multiple Bean Definitions

If multiple beans implement the same interface:

@Service
class StripePaymentService implements PaymentService {}

@Service
class PaypalPaymentService implements PaymentService {}

Spring doesn’t know which one to inject.

Fix using:

@Primary

or

@Qualifier("stripePaymentService")

Not sure when to use @Component, @Service, or @Repository?

Read:

@Component vs @Service vs @Repository – What Actually Changes?

Common Cause 5 — Static Fields

Spring cannot inject dependencies into static fields.

Incorrect:

@Autowired
private static EmailService service;

Correct:

@Autowired
private EmailService service;

Common Cause 6 — Objects Created Outside Spring

Objects created by:

  • Threads
  • Executors
  • Utility classes
  • Plain Java classes

are not managed by Spring.

Always inject dependencies into Spring-managed beans instead of manually created objects.

Constructor Injection vs Field Injection

Instead of field injection:

@Autowired
private UserRepository repository;

Prefer constructor injection:

@Service
public class UserService {

    private final UserRepository repository;

    public UserService(UserRepository repository) {
        this.repository = repository;
    }
}

Benefits include:

  • Easier unit testing
  • Immutable dependencies
  • Better readability
  • Reduced chance of NullPointerException

Constructor injection also makes Spring fail fast if dependencies cannot be created.

If bean creation fails, you’ll often see a BeanCreationException.

Learn how to diagnose it:

BeanCreationException in Spring Boot – Common Causes and Production Fixes

Production Best Practices

Follow these practices to avoid dependency injection issues in production:

  • Never instantiate Spring beans using new
  • Prefer constructor injection over field injection
  • Keep @SpringBootApplication in the root package
  • Avoid injecting static fields
  • Use @Qualifier or @Primary when multiple beans exist
  • Organize packages to simplify component scanning

Common Error Messages

The following exceptions are often related to incorrect dependency injection:

NullPointerException
UnsatisfiedDependencyException
NoSuchBeanDefinitionException
BeanCreationException

Understanding the root cause of these exceptions helps resolve startup failures much faster.

Related Spring Boot Guides

Continue learning with these in-depth guides:

Dependency Injection in Spring Boot

How Spring Boot Creates Beans (Bean Lifecycle Simplified)

BeanCreationException – Common Causes and Production Fixes

@Component vs @Service vs @Repository

How Spring Boot Application Starts

Spring Boot Configuration Loading Order

Frequently Asked Questions

Why is @Autowired null?

Usually because the object was created manually instead of by Spring, or because the required bean wasn’t registered in the Application Context.

Can Spring inject into static variables?

No. Spring does not support dependency injection into static fields.

Should I use field injection?

No. Constructor injection is the recommended approach for production applications because it improves testability, immutability, and makes missing dependencies obvious during startup.

Summary

Most @Autowired-related NullPointerExceptions are caused by incorrect dependency injection practices rather than bugs in Spring Boot. By ensuring your classes are managed by the Spring container, using constructor injection, organizing packages correctly, and understanding how Spring creates beans, you can eliminate these issues and build more reliable, production-ready applications.

Leave a Comment

Your email address will not be published. Required fields are marked *