๐ด The Error You're Seeing
Confirm this matches your console output. If it does, you're in the right place.
2026-02-14 18:40:05.200 ERROR 8842 --- [nio-8080-exec-8] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed: java.lang.NullPointerException: Cannot invoke "com.devinhyderabad.service.EmailService.sendEmail(String)" because "this.emailService" is null] with root cause
java.lang.NullPointerException: Cannot invoke "com.devinhyderabad.service.EmailService.sendEmail(String)" because "this.emailService" is nullโก Quick Fix Works 80% of the time
Stop using the 'new' keyword. Let Spring inject the dependency.
// BAD: UserController ctrl = new UserController();
// GOOD: Use @Autowired or constructor injection๐ ๏ธ Solutions (3 Ways to Fix)
Stop using 'new' for Spring Beans
๐ Use this if you manually instantiated a class containing @Autowired fields.
If you use 'new UserController()', Spring does not manage that object. Therefore, Spring will not inject its @Autowired dependencies, leaving them null.
// BAD
UserController ctrl = new UserController();
// GOOD (Let Spring inject it)
@RestController
public class UserApi {
@Autowired
private UserController ctrl; // Spring injects this
}Fix Component Scanning
๐ Use this if you aren't using 'new', but the bean still isn't injected.
Ensure the class you are trying to inject is actually a Spring Bean (annotated with @Service, @Component, etc.) and that it is in a package being scanned by @SpringBootApplication.
@Service // Ensure this annotation is present
public class EmailService { ... }Use Constructor Injection
๐ Use this to guarantee dependencies are injected at object creation time.
Instead of @Autowired on fields, use final fields and a constructor. This prevents the object from being created without its dependencies.
@RestController
public class UserController {
private final EmailService emailService;
// Spring injects this via constructor
public UserController(EmailService emailService) {
this.emailService = emailService;
}
}๐ Version Notes
Field injection is common, but discouraged.
Constructor injection is heavily favored. If a class has only one constructor, @Autowired is optional.
๐ง Why this Happens
Tap to expand the deep technical explanation
The object you are using was not instantiated by Spring. Because Spring didn't create it, Spring didn't process the @Autowired annotations, leaving the internal dependencies as null. When you try to call a method on that null dependency, you get a NullPointerException.
The HITEC City Parking Spot Analogy:
Imagine hiring a contractor to build a house, but you forget to give them the keys to the toolbox. They show up (the object is created), but they can't use any tools (dependencies are null) because the project manager (Spring) wasn't involved in their onboarding.
๐ก๏ธ How to Prevent This Next Time
Never use the 'new' keyword for classes that contain @Autowired fields. Always let the Spring IoC container manage those classes.