๐Ÿ”ด The Error You're Seeing

Confirm this matches your console output. If it does, you're in the right place.

ERROR LOG2026-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)

Solution 1โœ“ Most common cause

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 }
Solution 2

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 { ... }
Solution 3

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

Spring Boot 2.x

Field injection is common, but discouraged.

Spring Boot 3.x

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.

Course Search
Search across all chapters & stages
๐Ÿ“–

Search the course

Type any topic โ€” branching, stash, rebase, hooks โ€” and jump straight to that chapter.

merge branchesgit stashundo commitrebase