crossoverJie/JCSprout · error · NullPointerException

redisLimit is null

Error message

redisLimit is null

What it means

Thrown in the preHandle method of a SpringMVC HandlerInterceptor when the @Autowired RedisLimit field on the enclosing WebIntercept component is null at request time. The interceptor depends on RedisLimit to perform rate-limiting via redisLimit.limit(); a null reference means Spring never injected the bean, so the interceptor fails fast rather than letting an unrate-limited request through.

Source

Thrown at MD/distributed/Distributed-Limit.md:394

    @Autowired
    private RedisLimit redisLimit;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new CustomInterceptor())
                .addPathPatterns("/**");
    }


    private class CustomInterceptor extends HandlerInterceptorAdapter {
        @Override
        public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
                                 Object handler) throws Exception {


            if (redisLimit == null) {
                throw new NullPointerException("redisLimit is null");
            }

            if (handler instanceof HandlerMethod) {
                HandlerMethod method = (HandlerMethod) handler;

                ControllerLimit annotation = method.getMethodAnnotation(ControllerLimit.class);
                if (annotation == null) {
                    //skip
                    return true;
                }

                boolean limit = redisLimit.limit();
                if (!limit) {
                    logger.warn("request has bean limit");
                    response.sendError(500, "request limit");
                    return false;
                }

View on GitHub (pinned to fc4c6e5f6d)

Solutions

  1. Add or correct @ComponentScan(value = "com.crossoverjie.distributed.intercept") on your configuration class so Spring discovers WebIntercept.
  2. Ensure the module containing RedisLimit is on the classpath and that RedisLimit itself is annotated as a Spring bean.
  3. Verify Redis connection properties (host, port) are set and that the application started without Redis connection errors.
  4. Check the startup log for 'RedisLimit' bean creation failures or BeanCreationException.

Example fix

// before — missing component scan
@Configuration
public class AppConfig { }

// after
@Configuration
@ComponentScan(value = "com.crossoverjie.distributed.intercept")
public class AppConfig { }
Defensive patterns

Strategy: validation

Validate before calling

// At startup, verify the RedisLimit bean is present in the context.
@Autowired(required = false)
private RedisLimit redisLimit;

@PostConstruct
public void validate() {
    if (redisLimit == null) {
        throw new IllegalStateException(
            "RedisLimit not injected — add @ComponentScan(\"com.crossoverjie.distributed.intercept\") " +
            "and ensure Redis connection properties are set.");
    }
}

Try / catch

try {
    interceptor.preHandle(request, response, handler);
} catch (NullPointerException e) {
    if ("redisLimit is null".equals(e.getMessage())) {
        LOGGER.error("RedisLimit bean missing from Spring context — check @ComponentScan and Redis config", e);
        response.sendError(503, "Rate limiting unavailable");
        return;
    }
    throw e;
}

Prevention

When it happens

Trigger: The RedisLimit bean is not in the Spring context because: the com.crossoverjie.distributed.intercept package is not covered by @ComponentScan; RedisLimit lacks @Component or an @Bean factory method; or the Redis connection / RedisLimit auto-configuration failed silently during startup.

Common situations: Missing or incorrect @ComponentScan annotation (e.g. scanning the wrong package). RedisLimit is defined in a separate Maven/Gradle module that was not added as a dependency. Redis connection properties are missing so the RedisLimit conditional bean was not created. Profile misconfiguration where the bean is only registered under a specific profile.

Related errors


AI-assisted analysis of crossoverJie/JCSprout@fc4c6e5f6d (2026-08-14). Data as JSON: /api/errors/e61f159657801283. Report an issue: GitHub.