{"record":{"id":"c43716846b4667bc","repo":"crossoverJie/JCSprout","slug":"runnable-nullpointerexception","errorCode":null,"errorMessage":"runnable nullPointerException","messagePattern":"runnable nullPointerException","errorType":"exception","errorClass":"NullPointerException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/crossoverjie/concurrent/CustomThreadPool.java","lineNumber":117,"sourceCode":"     * @param callable\n     * @param <T>\n     * @return\n     */\n    public <T> Future<T> submit(Callable<T> callable) {\n        FutureTask<T> future = new FutureTask(callable);\n        execute(future);\n        return future;\n    }\n\n\n    /**\n     * 执行任务\n     *\n     * @param runnable 需要执行的任务\n     */\n    public void execute(Runnable runnable) {\n        if (runnable == null) {\n            throw new NullPointerException(\"runnable nullPointerException\");\n        }\n        if (isShutDown.get()) {\n            LOGGER.info(\"线程池已经关闭，不能再提交任务！\");\n            return;\n        }\n\n        //提交的线程 计数\n        totalTask.incrementAndGet();\n\n        //小于最小线程数时新建线程\n        if (workers.size() < miniSize) {\n            addWorker(runnable);\n            return;\n        }\n\n\n        boolean offer = workQueue.offer(runnable);\n        //写入队列失败","sourceCodeStart":99,"sourceCodeEnd":135,"githubUrl":"https://github.com/crossoverJie/JCSprout/blob/fc4c6e5f6d1772c2aec4c0f94cb3c8e7eb04018f/src/main/java/com/crossoverjie/concurrent/CustomThreadPool.java#L99-L135","documentation":"CustomThreadPool.execute() throws a NullPointerException when the submitted Runnable is null, mirroring the contract of java.util.concurrent.ThreadPoolExecutor.execute(). The pool needs a concrete task to schedule onto a worker thread, so a null reference is treated as a programmer error rather than silently ignored.","triggerScenarios":"Calling execute(null) directly, or submit(callable) where callable is null (submit wraps the callable in a FutureTask and forwards to execute). Also triggered when a method reference or supplier returns null and the result is passed in.","commonSituations":"Passing a conditional expression that evaluates to null (e.g. condition ? task : null). Submitting a task obtained from a factory or lookup that can return null. Copy-paste where the runnable variable was never assigned.","solutions":["Ensure the Runnable passed to execute()/submit() is never null — assign a no-op or throw a clearer IllegalArgumentException in your own code first.","If the task is optional, guard the call: if (runnable != null) pool.execute(runnable);","When calling submit(), validate the Callable argument before passing it."],"exampleFix":"// before\npool.execute(maybeNullTask);\n\n// after\nif (runnable != null) {\n    pool.execute(runnable);\n} else {\n    LOGGER.warn(\"skipping null task submission\");\n}","handlingStrategy":"validation","validationCode":"Objects.requireNonNull(runnable, \"runnable must not be null before submitting to pool\");\npool.execute(runnable);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always validate task arguments with Objects.requireNonNull before calling execute() or submit().","Avoid conditional expressions that can yield null (condition ? task : null); use an explicit if-guard instead.","If tasks come from a factory, have the factory return an empty Runnable rather than null."],"tags":["thread-pool","null-check","validation"],"backgroundTag":null,"analyzedSha":"fc4c6e5f6d1772c2aec4c0f94cb3c8e7eb04018f","analyzedAt":"2026-08-14T05:43:20.992Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}