{"record":{"id":"63015da4d353d741","repo":"lingochamp/FileDownloader","slug":"listener-must-not-be-null","errorCode":null,"errorMessage":"listener must not be null!","messagePattern":"listener must not be null!","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"library/src/main/java/com/liulishuo/filedownloader/event/DownloadEventPoolImpl.java","lineNumber":40,"sourceCode":"import java.util.HashMap;\nimport java.util.LinkedList;\nimport java.util.concurrent.Executor;\n\n/**\n * Implementing actions for event pool.\n */\npublic class DownloadEventPoolImpl implements IDownloadEventPool {\n\n    private final Executor threadPool = FileDownloadExecutors.newDefaultThreadPool(10, \"EventPool\");\n\n    private final HashMap<String, LinkedList<IDownloadListener>> listenersMap = new HashMap<>();\n\n    @Override\n    public boolean addListener(final String eventId, final IDownloadListener listener) {\n        if (FileDownloadLog.NEED_LOG) {\n            FileDownloadLog.v(this, \"setListener %s\", eventId);\n        }\n        if (listener == null) throw new IllegalArgumentException(\"listener must not be null!\");\n\n        LinkedList<IDownloadListener> container = listenersMap.get(eventId);\n\n        if (container == null) {\n            synchronized (eventId.intern()) {\n                container = listenersMap.get(eventId);\n                if (container == null) {\n                    listenersMap.put(eventId, container = new LinkedList<>());\n                }\n            }\n        }\n\n\n        synchronized (eventId.intern()) {\n            return container.add(listener);\n        }\n    }\n","sourceCodeStart":22,"sourceCodeEnd":58,"githubUrl":"https://github.com/lingochamp/FileDownloader/blob/6237a8cac174bcc916e4342b14ab1ab72a5768d4/library/src/main/java/com/liulishuo/filedownloader/event/DownloadEventPoolImpl.java#L22-L58","documentation":"DownloadEventPoolImpl.addListener() rejects null listener objects with an explicit IllegalArgumentException('listener must not be null!'). The event pool stores listeners per event id and cannot register a null callback.","triggerScenarios":"Calling downloadEventPool.addListener(eventId, null), or passing a listener variable/field that was never initialized (or set to null by a config/cleanup path).","commonSituations":"DI/config wiring where a listener bean failed to initialize; passing a listener obtained from a getter that returns null; clearing code that sets the listener field to null but the registration still runs; refactors that renamed a listener without updating the registration site.","solutions":["Pass a non-null IDownloadListener implementation to addListener","Null-check or early-return if your listener provider can be null: if (listener != null) pool.addListener(eventId, listener)","Fix the DI/wiring so the listener instance is created before registration","Log the call site (or assert) to find which code path passes null"],"exampleFix":"// before\npool.addListener(\"download.complete\", myListener); // myListener is null\n// after\nif (myListener == null) {\n    myListener = new DefaultDownloadListener();\n}\npool.addListener(\"download.complete\", myListener);","handlingStrategy":"type-guard","validationCode":"// before registering\nif (eventId == null || listener == null) {\n    throw new IllegalArgumentException(\"eventId and listener must be initialized before addListener\");\n}","typeGuard":"boolean canRegister(String eventId, IDownloadListener listener) {\n    return eventId != null && listener != null;\n}","tryCatchPattern":"try {\n    pool.addListener(eventId, listener);\n} catch (IllegalArgumentException e) {\n    log.error(\"addListener called with null listener; skipping registration\", e);\n}","preventionTips":["Initialize listener fields eagerly or make them final; avoid nullable listener variables","Null-check listeners returned from DI providers/getters before registering","Never pass a listener you previously set to null during cleanup without re-creating it","Centralize all addListener calls in one helper that guards null"],"tags":["android","filedownloader","null-argument","event-listener"],"backgroundTag":"null-argument","analyzedSha":"6237a8cac174bcc916e4342b14ab1ab72a5768d4","analyzedAt":"2026-09-08T23:50:48.168Z","contentChangedAt":"2026-09-08T23:50:48.168Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}