{"record":{"id":"e61f159657801283","repo":"crossoverJie/JCSprout","slug":"redislimit-is-null","errorCode":null,"errorMessage":"redisLimit is null","messagePattern":"redisLimit is null","errorType":"exception","errorClass":"NullPointerException","httpStatus":null,"severity":"error","filePath":"MD/distributed/Distributed-Limit.md","lineNumber":394,"sourceCode":"\n    @Autowired\n    private RedisLimit redisLimit;\n\n    @Override\n    public void addInterceptors(InterceptorRegistry registry) {\n        registry.addInterceptor(new CustomInterceptor())\n                .addPathPatterns(\"/**\");\n    }\n\n\n    private class CustomInterceptor extends HandlerInterceptorAdapter {\n        @Override\n        public boolean preHandle(HttpServletRequest request, HttpServletResponse response,\n                                 Object handler) throws Exception {\n\n\n            if (redisLimit == null) {\n                throw new NullPointerException(\"redisLimit is null\");\n            }\n\n            if (handler instanceof HandlerMethod) {\n                HandlerMethod method = (HandlerMethod) handler;\n\n                ControllerLimit annotation = method.getMethodAnnotation(ControllerLimit.class);\n                if (annotation == null) {\n                    //skip\n                    return true;\n                }\n\n                boolean limit = redisLimit.limit();\n                if (!limit) {\n                    logger.warn(\"request has bean limit\");\n                    response.sendError(500, \"request limit\");\n                    return false;\n                }\n","sourceCodeStart":376,"sourceCodeEnd":412,"githubUrl":"https://github.com/crossoverJie/JCSprout/blob/fc4c6e5f6d1772c2aec4c0f94cb3c8e7eb04018f/MD/distributed/Distributed-Limit.md#L376-L412","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Add or correct @ComponentScan(value = \"com.crossoverjie.distributed.intercept\") on your configuration class so Spring discovers WebIntercept.","Ensure the module containing RedisLimit is on the classpath and that RedisLimit itself is annotated as a Spring bean.","Verify Redis connection properties (host, port) are set and that the application started without Redis connection errors.","Check the startup log for 'RedisLimit' bean creation failures or BeanCreationException."],"exampleFix":"// before — missing component scan\n@Configuration\npublic class AppConfig { }\n\n// after\n@Configuration\n@ComponentScan(value = \"com.crossoverjie.distributed.intercept\")\npublic class AppConfig { }","handlingStrategy":"validation","validationCode":"// At startup, verify the RedisLimit bean is present in the context.\n@Autowired(required = false)\nprivate RedisLimit redisLimit;\n\n@PostConstruct\npublic void validate() {\n    if (redisLimit == null) {\n        throw new IllegalStateException(\n            \"RedisLimit not injected — add @ComponentScan(\\\"com.crossoverjie.distributed.intercept\\\") \" +\n            \"and ensure Redis connection properties are set.\");\n    }\n}","typeGuard":null,"tryCatchPattern":"try {\n    interceptor.preHandle(request, response, handler);\n} catch (NullPointerException e) {\n    if (\"redisLimit is null\".equals(e.getMessage())) {\n        LOGGER.error(\"RedisLimit bean missing from Spring context — check @ComponentScan and Redis config\", e);\n        response.sendError(503, \"Rate limiting unavailable\");\n        return;\n    }\n    throw e;\n}","preventionTips":["Add @ComponentScan covering the intercept and RedisLimit packages to your @Configuration / @SpringBootApplication.","Verify the Redis connection (host, port, password) is configured so RedisLimit can be constructed.","Add a startup assertion (@PostConstruct) to fail fast if redisLimit is null rather than discovering it at request time."],"tags":["spring","dependency-injection","rate-limiting","redis"],"backgroundTag":null,"analyzedSha":"fc4c6e5f6d1772c2aec4c0f94cb3c8e7eb04018f","analyzedAt":"2026-08-14T05:43:20.992Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}