🔴 The Error You're Seeing

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

ERROR LOG2026-02-20 10:05:22.120 ERROR 8842 --- [ main] o.s.boot.SpringApplication : Application run failed org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'healthCheckIndicator' defined in file [/com/devinhyderabad/health/HealthCheckIndicator.class]: Invocation of init method failed; nested exception is java.lang.NullPointerException at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1751)

⚡ Quick Fix Works 80% of the time

Ensure your HealthIndicator implementation does not throw exceptions in its constructor or health() method.

@Component public class HealthCheckIndicator implements HealthIndicator { @Override public Health health() { // Avoid null pointer exceptions here return Health.up().build(); } }

🧠 Why this Happens

Tap to expand the deep technical explanation

Spring Boot Actuator auto-detects any bean implementing `HealthIndicator`. If your custom indicator throws an exception during instantiation (e.g., a missing property injected via @Value, or a NullPointerException in the constructor), Spring fails to create the bean, which crashes the entire application context startup.

The HITEC City Parking Spot Analogy:

Imagine a hospital hiring a specialized doctor (HealthIndicator), but the doctor faints during their own onboarding physical. The hospital management (Spring) has to shut down the whole wing because the safety officer is unconscious.

🔁 How to Reproduce Confirm this is your error

Create a class implementing `HealthIndicator`. Autowire a missing property or throw a RuntimeException inside the `health()` method. Start the app.

🛠️ Solutions (5 Ways to Fix)

Solution 1✓ Most common cause

Fix the NullPointerException inside health()

👉 Use this if your health check logic relies on external services that might be null.

Wrap your health check logic in a try-catch block so it returns `Health.down()` instead of throwing an exception.

@Component public class HealthCheckIndicator implements HealthIndicator { @Override public Health health() { try { // risky logic return Health.up().build(); } catch (Exception e) { return Health.down(e).build(); } } }
Solution 2

Inject dependencies safely using @Lazy

👉 Use this if the bean fails because it depends on another bean that hasn't started yet.

Break the initialization cycle by lazily injecting the dependency.

@Component public class HealthCheckIndicator implements HealthIndicator { @Autowired @Lazy private ExternalService externalService; }
Solution 3

Fix missing @Value properties

👉 Use this if the bean uses @Value and the property is missing.

If `@Value("${api.url}")` is used, and the property is missing, the bean fails. Provide a default value.

@Value("${api.url:http://localhost:8080}") private String apiUrl;
Solution 4

Disable the specific health indicator

👉 Use this if the indicator is non-critical and you need the app to start immediately.

Tell Spring Boot to stop trying to instantiate this specific bean.

# application.properties management.health.custom.enabled=false
Solution 5

Separate business logic from health checks

👉 Use this if your health check is doing heavy database writes or complex logic.

Health indicators should only READ state. If you are modifying data, move that to a @Service method.

// HealthIndicator should only check status: public Health health() { return service.isConnected() ? Health.up().build() : Health.down().build(); }

📋 Version Notes

Spring Boot 2.x

Standard HealthIndicator interface.

Spring Boot 3.x

ReactiveHealthIndicator is preferred for WebFlux apps. Stricter init checks.

🛡️ How to Prevent This Next Time

Keep `HealthIndicator` implementations simple and defensive. Never let them throw unhandled exceptions.